Nitin Jaitely
Nitin Jaitely

Reputation: 15

Java parsing XML document gives “Content not allowed in prolog” error

I am trying to run a program in Java that takes a custom XML file and parses it. I'm using the XML file for storage. I am getting the following error in the error logs.

 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)

The beginning of the XML file is included:

<?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>

The program is able to read in the XML file. I get the error above. Here is a code snippet:

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

It doesn't seem to me that I have invalid content in the prolog of my XML file. I can't figure out what is wrong. Please help. Thanks.

Upvotes: 1

Views: 1821

Answers (2)

Koby
Koby

Reputation: 615

It usually means you have some hidden character at the beginning of the xml file.

Try opening it with an editor and view all characters and make sure there isn't a hidden character.

Upvotes: 1

GreenGiant
GreenGiant

Reputation: 5236

This error often means that the content you are trying to parse is either empty or is not well-formed XML.

In your case, it looks like some of your ending tags are missing. Where is the ending tag like </retrieveResponse> or </soapenv:Body>?

Upvotes: 0

Related Questions