“Content is not allowed in prolog” when parsing perfectly valid XML on GAE

“Content is not allowed in prolog” when parsing perfectly valid XML on GAE

Asked on January 9, 2019 in XML.
Add Comment


  • 7 Answer(s)

    The encoding in the XML and XSD or DTD are various.
         XML file header: <?xml version=’1.0′ encoding=’utf-8′?>
         XSD file header: <?xml version=’1.0′ encoding=’utf-16′?>

    The other scenario which causes this is when anything comes before the XML document type declaration. Since we might have something buffer like,

    helloworld<?xml version="1.0" encoding="utf-8"?>
    

         else a space or special character.

         There are some special characters called byte order markers which could be in the buffer. Do the below code before passing the buffer to the Parser,

    String xml = "<?xml ...";
    xml = xml.trim().replaceFirst("^([\\W]+)<","<");
    
    Answered on January 9, 2019.
    Add Comment

         The error message this is always created by the invalid XML content in the first element. For instance, extra small dot “.” in the beginning of XML element.

    The characters any of them before the “<?xml….” will cause above “org.xml.sax.SAXParseException: Content is not allowed in prolog” error message.

    A small dot “.” before the “<?xml…. to fix it, and delete just all those weird characters before the “<?xml“.

    Answered on January 9, 2019.
    Add Comment

         In some case, the XML files that were generated from c# program and stored into AS400 for later processing. After few analysis founded that was using UTF8 encoding while generating the XML files whereas javac in AS400 that uses “UTF8 without BOM”. So, need to write additional code parallel to below mentioned code,

    //create encoding with no BOM
    Encoding outputEnc = new UTF8Encoding(false);
    //open file with encoding
    TextWriter file = new StreamWriter(filePath, false, outputEnc);
     
    file.Write(doc.InnerXml);
    file.Flush();
    file.Close(); // save and close it
    
    Answered on January 9, 2019.
    Add Comment
    <?xml version="1.0" encoding="utf-8"?> 
    <ListDomainsResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/">
        <ListDomainsResult>
            <DomainName>Audio</DomainName>
            <DomainName>Course</DomainName>
            <DomainName>DocumentContents</DomainName>
            <DomainName>LectureSet</DomainName>
            <DomainName>MetaData</DomainName>
            <DomainName>Professors</DomainName>
            <DomainName>Tag</DomainName>
        </ListDomainsResult>
        <ResponseMetadata>
            <RequestId>42330b4a-e134-6aec-e62a-5869ac2b4575</RequestId>
            <BoxUsage>0.0000071759</BoxUsage>
        </ResponseMetadata>
    </ListDomainsResources
    
    Answered on February 25, 2019.
    Add Comment
    com.amazonaws.http.HttpClient handleResponse: Unable to unmarshall response (ParseError at [row,col]:[1,1]
    Message: Content is not allowed in prolog.): <?xml version="1.0" encoding="utf-8"?> 
    <ListDomainsResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/"><ListDomainsResult><DomainName>Audio</DomainName><DomainName>Course</DomainName><DomainName>DocumentContents</DomainName><DomainName>LectureSet</DomainName><DomainName>MetaData</DomainName><DomainName>Professors</DomainName><DomainName>Tag</DomainName></ListDomainsResult><ResponseMetadata><RequestId>42330b4a-e134-6aec-e62a-5869ac2b4575</RequestId><BoxUsage>0.0000071759</BoxUsage></ResponseMetadata></ListDomainsResponse>
    javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
    Message: Content is not allowed in prolog.
        at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
        at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(Unknown Source)
        at com.amazonaws.transform.StaxUnmarshallerContext.nextEvent(StaxUnmarshallerContext.java:153)
        
    Answered on February 25, 2019.
    Add Comment
    String xml = "<?xml ...";
    xml = xml.trim().replaceFirst("^([\\W]+)<","<");
    Answered on February 25, 2019.
    Add Comment
    //create encoding with no BOM
    Encoding outputEnc = new UTF8Encoding(false); 
    //open file with encoding
    TextWriter file = new StreamWriter(filePath, false, outputEnc);           
    
    file.Write(doc.InnerXml);
    file.Flush();
    file.Close(); // save and close it
    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.