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

前端 未结 30 1830
别那么骄傲
别那么骄傲 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:44

    I followed the instructions found here and i got the same error.

    I tried several things to solve it (ie changing the encoding, typing the XML file rather than copy-pasting it ect) in Notepad and XML Notepad but nothing worked.

    The problem got solved when I edited and saved my XML file in Notepad++ (encoding --> utf-8 without BOM)

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

    As Mike Sokolov has already pointed it out, one of the possible reasons is presence of some character/s (such as a whitespace) before the tag.

    If your input XML is being read as a String (as opposed to byte array) then you can use replace your input string with the below code to make sure that all 'un-necessary' characters before the xml tag are wiped off.

    inputXML=inputXML.substring(inputXML.indexOf("<?xml"));
    

    You need to be sure that the input xml starts with the xml tag though.

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

    To fix the BOM issue on Unix / Linux systems:

    1. Check if there's an unwanted BOM character: hexdump -C myfile.xml | more An unwanted BOM character will appear at the start of the file as ...<?xml>

    2. Alternatively, do file myfile.xml. A file with a BOM character will appear as: myfile.xml: XML 1.0 document text, UTF-8 Unicode (with BOM) text

    3. Fix a single file with: tail -c +4 myfile.xml > temp.xml && mv temp.xml myfile.xml

    4. Repeat 1 or 2 to check the file has been sanitised. Probably also sensible to do view myfile.xml to check contents have stayed.

    Here's a bash script to sanitise a whole folder of XML files:

    #!/usr/bin/env bash
    
    # This script is to sanitise XML files to remove any BOM characters
    
    has_bom() { head -c3 "$1" | LC_ALL=C grep -qe '\xef\xbb\xbf'; }
    
    for filename in *.xml ; do
      if has_bom ${filename}; then
        tail -c +4 ${filename} > temp.xml
        mv temp.xml ${filename}
      fi
    done
    
    
    0 讨论(0)
  • 2020-11-22 03:48

    Try adding a space between the encoding="UTF-8" string in the prolog and the terminating ?>. In XML the prolog designates this bracket-question mark delimited element at the start of the document (while the tag prolog in stackoverflow refers to the programming language).

    Added: Is that dash in front of your prolog part of the document? That would be the error there, having data in front of the prolog, -<?xml version="1.0" encoding="UTF-8"?>.

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

    I had the same problem with some XML files, I solved reading the file with ANSI encoding (Windows-1252) and writing a file with UTF-8 encoding with a small script in Python. I tried use Notepad++ but I didn't have success:

    import os
    import sys
    
    path = os.path.dirname(__file__)
    
    file_name = 'my_input_file.xml'
    
    if __name__ == "__main__":
        with open(os.path.join(path, './' + file_name), 'r', encoding='cp1252') as f1:
            lines = f1.read()
            f2 = open(os.path.join(path, './' + 'my_output_file.xml'), 'w', encoding='utf-8')
            f2.write(lines)
            f2.close()
    
    0 讨论(0)
  • 2020-11-22 03:48

    We had the same problem recently and it turned out to be the case of a bad URL and consequently a standard 403 HTTP response (which obviously isn't the valid XML the client was looking for). I'm going to share the detail in case someone within the same context run into this problem:

    This was a Spring based web application in which a "JaxWsPortProxyFactoryBean" bean was configured to expose a proxy for a remote port.

    <bean id="ourPortJaxProxyService"
        class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean"
        p:serviceInterface="com.amir.OurServiceSoapPortWs"
        p:wsdlDocumentUrl="${END_POINT_BASE_URL}/OurService?wsdl"
        p:namespaceUri="http://amir.com/jaxws" p:serviceName="OurService"
        p:portName="OurSoapPort" />
    

    The "END_POINT_BASE_URL" is an environment variable configured in "setenv.sh" of the Tomcat instance that hosts the web application. The content of the file is something like this:

    export END_POINT_BASE_URL="http://localhost:9001/BusinessAppServices"
    #export END_POINT_BASE_URL="http://localhost:8765/BusinessAppServices"
    

    The missing ";" after each line caused the malformed URL and thus the bad response. That is, instead of "BusinessAppServices/OurService?wsdl" the URL had a CR before "/". "TCP/IP Monitor" was quite handy while troubleshooting the problem.

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