Org. xml. sax. SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog, saxparseexception

Source: Internet
Author: User

Org. xml. sax. SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog, saxparseexception

This exception often occurs when reading xml files using sax. The reason for reading xml files is:

1. BOM

Test whether bom in inputstream can use org. apache. commons. IO. input. BOMInputStream of apache commons io in java. If your project has introduced IO

BOM basic knowledge can refer to: http://www.unicode.org/faq/utf_bom.html

Add a small method to filter BOM.

private static InputStream checkForUtf8BOMAndDiscardIfAny(InputStream inputStream) throws IOException {    PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);    byte[] bom = new byte[3];    if (pushbackInputStream.read(bom) != -1) {        if (!(bom[0] == (byte) 0xEF && bom[1] == (byte) 0xBB && bom[2] == (byte) 0xBF)) {            pushbackInputStream.unread(bom);        }    }    return pushbackInputStream; }


2. Unstructured xml content, for example, in <? Invalid characters before xml

You can use a regular expression <? Start to intercept content in xml

3. What I found today is that: The Accept-Encoding enables compression transmission. You can try to set Accept-Encoding to identity, and check whether Chunked Transfer Encoding is used)


Later, it was said that it could be filtered out to pass the Code directly to the sax parser. Of course, the sax and jaxp I used won't be filtered out. Test environment: java 7 + xerces sax. All exception stacks

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.