android - 为什么此代码不断触发 SaxParseException : ""PI must not start with xml"?

标签 android xml saxparseexception

此代码用于从其字符串表示形式生成 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/

相关文章:

java - 使用任务 :annotation-driven tag in Spring MVC 时出现 SAXParseException

java - 如何将文件写入 Android 中的外部公共(public)存储以便它们在 Windows 中可见?

html - 如何对特定文本使用 XPath contains()?

php - CakePHP 站点地图错误

java - SAXParseException;无效的编码名称 "UTF8"

java - 如何查找同一 xml 元素 id 值的所有多次使用?

android - 低于 Lollipop 的 Android 版本上的 java.lang.NoClassDefFoundError

java - 如何在不使用数字格式的情况下每 3 位添加逗号?

android uiautomator : UIObject. 带有日文字符的 setText 失败

c# - 从 XML 文件的最后一个子级中选择值