Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

xml - java - SAXparserException: content is not allowed in prolog

I am getting exception while trying to parse an XML document.

I went through many post like here and here. But still my problem not got solved. I checked i dont have any spaces too in header.I created it with notepad and i choosed encoding as utf-8 while saving.

My XML file looks like this

<?xml version="1.0" encoding="UTF-8"?>
<poem>
<title>Roses are Red</title>
<l>Roses are red</l>
</poem>

I am using java to load the file and parse it. My java code is

File xml = new File("d:\uploads	est.xml");
try{  
     XMLReader xr = XMLReaderFactory.createXMLReader();
     MySAXApp handler = new MySAXApp();
     xr.setContentHandler(handler);
     xr.setErrorHandler(handler);
     FileReader r = new FileReader(xml);
     xr.parse(new InputSource(r));
 }
 catch(Exception e)
 {
      log.info("Exception : "+e.getMessage());
 }

My MySAXApp class is below

package utility;
import java.io.FileReader;
import java.util.logging.Logger;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;

public class MySAXApp extends DefaultHandler {

    public Logger log;
    public MySAXApp ()
    {

        super();
        log = Logger.getAnonymousLogger();
    }
    public void startDocument ()
    {
        log.info("Start document");
    }


    public void endDocument ()
    {
        log.info("End document");
    }
    public void startElement (String uri, String name,String qName, Attributes atts)
    {

      log.info("Start element: " + qName);

    }

    public void endElement (String uri, String name, String qName)
    {
         log.info("End element: " + qName);
    }
    public void characters (char ch[], int start, int length)
    {
        log.info("values:    "");
        for (int i = start; i < start + length; i++) {
            switch (ch[i]) {
            case '\':
            log.info("");
            break;
            case '"':
            log.info(""");
            break;
            case '
':
            log.info("\n");
            break;
            case '
':
            log.info("\r");
            break;
            case '	':
            log.info("\t");
            break;
            default:
            log.info(ch[i]+"");
            break;
            }
        }
        log.info(""
");
    }
}

Stack trace

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)
    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:1411)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1038)
    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 utility.PerformOperation.startIndexing(PerformOperation.java:91)
    at utility.Upload.doPost(Upload.java:126)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is explained by @MikeSokolov in the first of your links. Quote:

Another thing that often happens is a UTF-8 BOM (byte order mark), which is allowed before the XML declaration can be treated as whitespace if the document is handed as a stream of characters to an XML parser rather than as a stream of bytes.

FileReader reads the file as a character stream, and to read the file as a byte stream, you should use FileInputStream instead, as follows:

FileInputStream is = new FileInputStream(xml);
xr.parse(new InputSource(is));

If you examine your text file in a hex editor you will see the UTF-8 BOM at the start (EF BB BF) and it is this that is causing the problem when using FileReader.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...