androidxmljdomnon-well-formed

can't read xml file on android


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.


Solution

  • 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()