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)