为什么这个code保持触发的SAXParseException:"" PI不能与XML&QUOT开始;?能与、SAXParseException、code、QUOT

2023-09-07 22:02:41 作者:、我媳婦用不着藍顏寵i

这code被用于从它的字符串重新presentation一个XML文档。它工作在我小的单元测试正常,但在我的实际的XML数据失败。它触发线是文档的DOC = db.parse(是);

任何想法?

 公共静态文档FromString(字符串XML){    //从http://www.rgagnon.com/javadetails/java-0573.html    尝试    {        DBF的DocumentBuilderFactory = DocumentBuilderFactory.newInstance();        的DocumentBuilder分贝= dbf.newDocumentBuilder();        InputSource的是=新的InputSource();        is.setCharacterStream(新StringReader(XML));        文档的DOC = db.parse(是);        doc.normalize();        返回文档;    }    赶上(例外五)    {        Log.WriteError(无法解析XML,即XML.FromString(字符串));        返回null;    }} 

解决方案 vscode配置C C windows编译环境 楚来客的专栏 CSDN博客

感谢您的帮助大家。

我丢弃<?XML版本=1.0编码=UTF-8> 来清除此错误。还是不明白这样做的原因可能是,但它仍然工作。

我又找到我缓冲的作家之一(从一个zip文件到内存解压时)没有被刷新,这是导致XML字符串是不完整的。

谢谢大家对你的帮助!

This code is used to generate a XML document from its String representation. It works fine in my small unit tests, but fails in my actual xml data. The line where it triggers is Document doc = db.parse(is);

Any ideas?

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;
    }
}

解决方案

Thanks for your help everyone.

I discarded the <?xml version="1.0" encoding="utf-8"?> which cleared this error. Still don't understand what the reason for this might be, but it worked nonetheless.

I went on to find one of my buffered writers (when extracting from a zip file into memory) wasn't being flushed, which was causing the xml string to be incomplete.

Thanks everyone for your help!

 
精彩推荐