我有一个包含 xml 内容的字符串,如下所示:
String xml = "<item_list>" +
"<category id='2' name='categoryName'>" +
"<item id='41' name='item1' />" +
"</category>" +
"</item_list>)";
我想将其转换为 Document 对象。这是我的代码:
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);
当我运行它时,我得到下一个错误:
org.xml.sax.SAXParseException: Unexpected token (position:TEXT )@1:139 in java.io.InputStreamReader@40fa7860)
我究竟做错了什么?