此代码用于从其字符串表示形式生成 XML 文档。它在我的小型单元测试中运行良好,但在我的实际 xml 数据中却失败了。它触发的行是 Document doc = db.parse(is);
有什么想法吗?
public static Document FromString(String xml)
{
// from http://www.rgagnon.com/javadetails/java-0573.html
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
Document doc = db.parse(is);
doc.normalize();
return doc;
}
catch (Exception e)
{
Log.WriteError("Failed to parse XML", e, "XML.FromString(String)");
return null;
}
}
最佳答案
谢谢大家的帮助。
我丢弃了 <?xml version="1.0" encoding="utf-8"?>
清除了这个错误。仍然不明白这可能是什么原因,但它仍然有效。
我接着发现我的一个缓冲写入器(当从 zip 文件提取到内存中时)没有被刷新,这导致 xml 字符串不完整。
感谢大家的帮助!
关于android - 为什么此代码不断触发 SaxParseException : ""PI must not start with xml"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4988145/