1

I am trying to open and read a xml file as a local resource with jdom. I can open the file but then I got an error saying that the xml file is not well-formed. Here is the code :

Uri path = Uri.parse("android:resource://package_name"+R.xml.data);
InputSource source = new InputSource(new StringReader(path.toString()));
document = builder.build(source);

I got the following error :

org.jdom.input.JDOMParseException:Error on line 1:At line 1, column 17:not well-formed (invalid token)

Here is the xml file with my data:

<?xml version="1.0" encoding="utf-8"?>
<data>
  <person>
    <name>Name1</name>
    <firstname>Firstname1</firstname>
  </person>
  <person>
    <name>Name2</name>
    <firstname>Firstname2</firstname>
  </person>
</data>

Would you have an idea ? Thanks in advance.

user1086225
  • 45
  • 1
  • 4

2 Answers2

4

Your InputSource is not reading the resource, it is reading the path to the resource (which is not a well-formed XML file :))

If you are including an XML resource in Android, it will be pre-parsed at build time, so you don't want to use JDOM to read it. (If you really want to read it into a DOM, include it as a raw resource)

Instead, use Resources.getXml(R.xml.data), which will return a XmlPullParser

If you need it in a DOM, put it with the raw/ resources, and get your reader for your InputSource from openRawResource()

antlersoft
  • 14,636
  • 4
  • 35
  • 55
2

I'm not sure you've referenced the file correctly, as the XML file seems fine. I'd check the path that you specify to the file, as the R.xml.data will just return an int, not a file name.

Davos555
  • 1,974
  • 15
  • 23