我尝试解析 XML 并得到以下错误,问题可能是什么?
I/System.out(8058):错误的 XML 文件结构:意外的标记(位置:TEXT @1:2 in java.io.StringReader@4113db88)
这就是我解析的方法
public final static Document XMLfromString(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}
return doc;
}
这就是我尝试解析的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<app>
<Date Value="02.07.2012">
</Date>
</app>