2

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?

nikmin
  • 1,803
  • 3
  • 28
  • 46

2 Answers2

2

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/

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

"</item_list>)";

That parenthesis shouldn't be there.

dmon
  • 30,048
  • 8
  • 87
  • 96