0

I have a problem with parsing XML file on android.

My examples.xml file look like this:

<categories>
    <example>something</example>
</categories>

And my Java code:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(getResources().openRawResource(R.xml.examples));
doc.getDocumentElement().normalize(); //here it throws NullPointerException

It throws a NullPointerException when I try to normalize().

I am inspired by this simple tutorial http://sanjaal.com/java/tag/getdocumentelementnormalize/

Can anyone tell me what I'm doing wrong? Thanks

1
  • 2
    the value of doc is most likely null Commented Jun 1, 2011 at 16:27

3 Answers 3

2

Try moving your xml file to the res/raw folder, then try this:

Document doc = db.parse(getResources().openRawResource(R.raw.examples));

Let me know if that works.

EDIT: if you look at http://developer.android.com/reference/android/content/res/Resources.html#openRawResource%28int%29 it basically says you can only use that on drawable, sound and raw resources.

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

Comments

0

Try XmlPullParser. It's a little bit easier, I think.

XmlPullParser | Android Developers

Comments

0

Try Project|Clean - it might not be getting the raw XML from your resources. Eclipse seems a bit flaky that way, I sometimes change code & it doesn't pick up the changes until its cleaned.

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.