0

I have an XML file /res/raw/myxml.xml

I want to parse it with DOM Parser

I used this documentation but my problem how to read this XML file in local

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = mcontext.getResources(R.raw.myxml);
        doc.getDocumentElement().normalize();

I have an error in this line Document doc = mcontext.getResources(R.raw.myxml); in mcontext the error message is mcontext cannot be resolved

I want to parse this document with DOM not with SAX

I try the SAX Parser but with it i have a problem with execution of an XPATH query, for this I changed the parser

3
  • It would be helpful to see the text of the error messages. Commented Apr 2, 2011 at 12:20
  • the error message is mcontext cannot be resolved Commented Apr 2, 2011 at 12:22
  • 1
    Thank you, too. You did 29 questions so far on StackOverflow and mostly you got an answer but you didn't chose any answer as the solution. You motivate programmers to answer your questions if you chose a correct answer since those persons will be rewarded with points that can be used for e. g. pushing up their own questions. See here: stackoverflow.com/faq Commented Apr 2, 2011 at 12:45

2 Answers 2

2
getResources().openRawResource(R.raw.myxml);

Use this, it worked for my code but I used this with InputStream. Maybe it also works for your code.

Sign up to request clarification or add additional context in comments.

Comments

0

I have an XML file /res/raw/myxml.xml. I want to pase it with DOM Parser

Why? Put the XML in res/xml/ and get at it via getXml() on a Resources object. For compiled XML resources, this will be about 10 times faster than SAX and probably 30-40 times faster than DOM. This will run faster and consume less battery life. Here is a sample project demonstrating this.

I have an error in this line Document doc = mcontext.getResources(R.raw.myxml); in mcontext the error message is mcontext cannot be resolved

That is because you do not have an mcontext variable or data member defined. Newcomers to Java should learn Java before trying to develop in Java for Android.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.