Java解析XML文档提供了“prolog中不允许的内容”错误

时间:2015-08-21 16:56:03

标签: java xml eclipse xml-parsing

我正在尝试在Java中运行一个程序,它接受一个自定义XML文件并解析它。我正在使用XML文件进行存储。我在错误日志中收到以下错误。

 WARN - OtherFault:
javax.xml.bind.UnmarshalException: Content is not allowed in prolog.
 - with linked exception:
[org.xml.sax.SAXParseException: Content is not allowed in prolog.]
        at com.wsc.cca.ns20100530.fault.xml.impl.runtime.SAXUnmarshallerHandlerImpl.handleEvent(Unknown Source)
        at com.wsc.cca.ns20100530.fault.xml.impl.runtime.ErrorHandlerAdaptor.propagateEvent(Unknown Source)
        at com.wsc.cca.ns20100530.fault.xml.impl.runtime.ErrorHandlerAdaptor.fatalError(Unknown Source)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
     at com.wsc.cca.ns20100530.fault.xml.impl.runtime.ErrorHandlerAdaptor.fatalError(Unknown Source)
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
        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:1414)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1039)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
        at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
        at com.wsc.cca.ns20100530.fault.xml.impl.runtime.UnmarshallerImpl.unmarshal(Unknown Source)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
        at com.cts.util.CCARetrieveThread.run(CCARetrieveThread.java:325)
        at java.lang.Thread.run(Thread.java:619)
Caused by: 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)

包含XML文件的开头:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<retrieveResponse xmlns="urn:wsc.com/cca/2010/05/30">
    <Number>1234</Number>
    <Info>
        <ID>12</ID>
        <Status>ACTIVE</Status>
        <ClosedDate xsi:nil="true"/>
        <Info>
            <To>1</To>
            <From>2</From>
            <Date>2011-05-16-04:00</Date>
        </Info>
        <Multi>true</Multi>
        <CardExpiryDate>2012-12-03-05:00</CardExpiryDate>
    </Info>
    <PInfo>
        <PID>000005471</PID>
        <Title xsi:nil="true"/>
        <FName>TAYLOR</FName>
        <LName>NGWHIP</LName>
        <Language>en</Language>
        <PRelationship>PRIMARY</PRelationship>
        <Number>1234</Number>
        <TravelVIP xsi:nil="true"/>
        <DesignatedUser1 xsi:nil="true"/>
            <DesignatedUser2 xsi:nil="true"/>
        </PInfo>
</retrieveResponse>
</soapenv:Body></soapenv:Envelope>

该程序能够读取XML文件。我得到上面的错误。 这是一段代码:

if (pAWSType == Constants.AWS_CASHBACK) {
    sLogger.info("Setting stub parameter for Cashback");
    File lStubFile = new File(lConfig.getString("com.CCAWS.STUB.FilePath")+"CCBResponse-"+pAcctNum+".xml");
    if (!lStubFile.exists()) {
        mRetrieveThread = new CCARetrieveThread(new File(lConfig.getString("com.CCAWS.STUB.FilePath")+"CCBResponse.xml"));      
    } else {
        mRetrieveThread = new CCARetrieveThread(lStubFile);
    }
} else {
    sLogger.info("Setting stub parameter for Travel Points");
    File lStubFile = new File(lConfig.getString("com.CCAWS.STUB.FilePath")+"CCAResponse-"+pAcctNum+".xml");
    if (!lStubFile.exists()) {
        mRetrieveThread = new CCARetrieveThread(new File(lConfig.getString("com.CCAWS.STUB.FilePath")+"CCAResponse.xml"));      
    } else {
        mRetrieveThread = new CCARetrieveThread(lStubFile);
    }
}

在我看来,我的XML文件的prolog中包含无效内容。我无法弄清楚出了什么问题。请帮忙。感谢。

2 个答案:

答案 0 :(得分:1)

这通常意味着您在xml文件的开头有一些隐藏的字符。

尝试使用编辑器打开它并查看所有字符并确保没有隐藏字符。

答案 1 :(得分:0)

此错误通常意味着您尝试解析的内容为空或者格式不正确的XML。

在您的情况下,看起来您的某些结尾标记丢失了。结束标记在哪里,如</retrieveResponse></soapenv:Body>