org.xml.sax.SAXParseException: Content is not allowed in prolog

前端 未结 30 1781
别那么骄傲
别那么骄傲 2020-11-22 02:54

I have a Java based web service client connected to Java web service (implemented on the Axis1 framework).

I am getting following exception in my log file:

相关标签:
30条回答
  • 2020-11-22 03:42

    For the same issues, I have removed the following line,

      File file = new File("c:\\file.xml");
      InputStream inputStream= new FileInputStream(file);
      Reader reader = new InputStreamReader(inputStream,"UTF-8");
      InputSource is = new InputSource(reader);
      is.setEncoding("UTF-8");
    

    It is working fine. Not so sure why that UTF-8 gives problem. To keep me in shock, it works fine for UTF-8 also.

    Am using Windows-7 32 bit and Netbeans IDE with Java *jdk1.6.0_13*. No idea how it works.

    0 讨论(0)
  • 2020-11-22 03:43

    In my case, removing the 'encoding="UTF-8"' attribute altogether worked.

    It looks like a character set encoding issue, maybe because your file isn't really in UTF-8.

    0 讨论(0)
  • 2020-11-22 03:43

    My answer wouldn't help you probably, but it help with this problem generally.

    When you see this kind of exception you should try to open your xml file in any Hex Editor and sometime you can see additional bytes at the beginning of the file which text-editor doesn't show.

    Delete them and your xml will be parsed.

    0 讨论(0)
  • 2020-11-22 03:43

    Sometimes it's the code, not the XML

    The following code,

    Document doc = dBuilder.parse(new InputSource(new StringReader("file.xml")));
    

    will also result in this error,

    [Fatal Error] :1:1: Content is not allowed in prolog.org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

    because it's attempting to parse the string literal, "file.xml" (not the contents of the file.xml file) and failing because "file.xml" as a string is not well-formed XML.

    Fix: Remove StringReader():

    Document doc = dBuilder.parse(new InputSource("file.xml"));
    

    Similarly, dirty buffer problems can leave residual junk ahead of the actual XML. If you've carefully checked your XML and are still getting this error, log the exact contents being passed to the parser; sometimes what's actually being (tried to be) parsed is surprising.

    0 讨论(0)
  • 2020-11-22 03:43

    I had the same issue.

    First I downloaded the XML file to local desktop and I got Content is not allowed in prolog during the importing file to portal server. Even visually file was looking good to me but somehow it's was corrupted.

    So I re-download the same file and tried the same and it worked.

    0 讨论(0)
  • 2020-11-22 03:43

    I was having the same problem while parsing the info.plist file in my mac. However, the problem was fixed using the following command which turned the file into an XML.

    plutil -convert xml1 info.plist
    

    Hope that helps someone.

    0 讨论(0)
提交回复
热议问题