简体   繁体   English

解析xml时出现SAXParseException

[英]SAXParseException while parsing xml

    foundation.NSPropertyListSerialization$_XML$DictionaryParser.fatalError- Parse fatal error : 
org.xml.sax.SAXParseException: Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:174)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:388)
    at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1476)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1037)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:647)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:513)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:815)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:744)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:128)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1208)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:543)
    at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)

This question has multiple answers - Content is not allowed in Prolog SAXParserException and Java parsing XML document gives "Content not allowed in prolog." 这个问题有多个答案-Prolog SAXParserException中不允许使用内容,并且Java解析XML文档给出“ Prolog中不允许使用的内容”。 error 错误

I am not able to resolve the error despite referring to all the posts possible. 尽管引用了所有可能的帖子,但我无法解决该错误。 I also tried the solution recommended here - http://www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html 我还尝试了此处推荐的解决方案-http: //www.rgagnon.com/javadetails/java-handle-utf8-file-with-bom.html

I combined both the answers, and did this - 我结合了两个答案,并做到了-

String inputBytesToStr = new String(inputBytes);
if (inputBytesToStr.startsWith("\uFEFF")) {
            inputBytesToStr = inputBytesToStr.substring(1);             
        }
        inputBytesToStr = inputBytesToStr.trim().replaceFirst("^([\\W]+)<","<");
        inputBytesToStr = inputBytesToStr.replaceAll("[^\\x20-\\x7e\\x0A]", "");

Here is my xml 这是我的xml

    <?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<info>
    <key>Name</key>
    <string>Patrick</string>
    <key>Country</key>
    <string>Australia</string>
    <array>
        <string>myImageOne.jpeg</string>
        <string>myImageTwo.jpeg</string>
        <string>myImageOne.jpeg</string>
    </array>
</info>
</plist>

Still no luck. 仍然没有运气。 I've been working on it for the past 12 hours. 在过去的12个小时中,我一直在努力。 I need to resolve this now. 我现在需要解决这个问题。 PLEASE help me. 请帮我。

Given this is the standard Apple .plist file format you may have better luck using a dedicated library designed for plist files such as https://code.google.com/p/plist/ instead of trying to parse it by hand. 鉴于这是标准的Apple .plist文件格式,您可以使用专为plist文件设计的专用库(例如https://code.google.com/p/plist/)来获得更好的运气,而不是尝试手动解析它。

Using a library would also have the advantage that it could cope transparently with plist files in the binary serialization format as well as the XML form - the same file extension is used for both and you can't tell which format a particular plist is in until you try opening it. 使用库还具有以下优点:它可以透明地处理二进制序列化格式以及XML格式的plist文件-两者都使用相同的文件扩展名,直到您知道特定的plist处于哪种格式为止您尝试打开它。

我唯一遇到过解析语法的人是dd-plist: https : //github.com/3breadt/dd-plist ,并且花了很多时间尝试各种选项,值得一提。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM