Question

I have a string that's has xml content in it like this:

String xml = "<item_list>" +
                "<category id='2' name='categoryName'>" +
                   "<item id='41' name='item1' />" +
                "</category>" +
             "</item_list>)";

I want to convert this to an Document object. Here my code for doing this:

Document doc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
doc = builder.parse(is);

When I run this I get the next error:

org.xml.sax.SAXParseException: Unexpected token (position:TEXT )@1:139 in java.io.InputStreamReader@40fa7860)

What am I doing wrong?

Was it helpful?

Solution

Change your xml string as:

String xml = "<?xml version='1.0' encoding='UTF-8'?>" +
                "<item_list>" +
                "<category id='2' name='categoryName'>" +
                   "<item id='41' name='item1' />" +
                "</category>" +
             "</item_list>";

Currently you are missing xml document header <?xml version='1.0' encoding='UTF-8'?> in your string

for more help how we create xml doc in android see this tutorial :

http://xjaphx.wordpress.com/2011/10/27/android-xml-adventure-create-write-xml-data/

OTHER TIPS

"</item_list>)";

That parenthesis shouldn't be there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top