Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我会得到org.xml.sax.SAXParseException;prolog中不允许包含内容?_Java_Xml_Xml Parsing_Sax - Fatal编程技术网

Java 为什么我会得到org.xml.sax.SAXParseException;prolog中不允许包含内容?

Java 为什么我会得到org.xml.sax.SAXParseException;prolog中不允许包含内容?,java,xml,xml-parsing,sax,Java,Xml,Xml Parsing,Sax,我试图解析一个xml文件,但它不断地出现异常。我在这里寻找一些解决办法,因为很多人和我有几乎相同的问题,但都是徒劳的。请求和响应似乎是正确的,我已经检查了我的xml,没有发现任何错误 以下是我的Java代码: public static void main(String args[]) { SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFa

我试图解析一个xml文件,但它不断地出现异常。我在这里寻找一些解决办法,因为很多人和我有几乎相同的问题,但都是徒劳的。请求和响应似乎是正确的,我已经检查了我的xml,没有发现任何错误

以下是我的Java代码:

             public static void main(String args[])
             {
                SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
                SOAPConnection soapConnection = soapConnectionFactory.createConnection();

                // Send SOAP Message to SOAP Server

                String url = "https://sync-test.severa.com/webservice/S3/API.svc";
                SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

                XMLTransform(soapResponse.toString());
             }

             private static SOAPMessage createSOAPRequest()
             {
             MessageFactory messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();
            String serverURI = "http://soap.severa.com/";
            // SOAP Envelope
            SOAPEnvelope envelope = soapPart.getEnvelope();
            //SOAP Header content
            SOAPFactory soapFactory = SOAPFactory.newInstance();
            SOAPHeader header = soapMessage.getSOAPHeader();
            Name headerName = soapFactory.createName("WebServicePassword", "ns1", serverURI);
            SOAPHeaderElement headerElement = header.addHeaderElement(headerName);
            headerElement.addTextNode("uheyhd80984023984209380");


            // SOAP Body

            SOAPBody soapBody = envelope.getBody();
            //Create GetAllInvoiceStatuses
            Name name = envelope.createName("GetAllInvoiceStatuses", "ns1", serverURI);
            SOAPElement child = soapBody.addBodyElement(name);
            child.addNamespaceDeclaration("ns1", serverURI);
            MimeHeaders headers = soapMessage.getMimeHeaders();
            headers.addHeader("SOAPAction", "http://soap.severa.com/IInvoiceStatus/GetAllInvoiceStatuses");

            }

            private static void  XMLTransform (String xml) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(xml.toString()));
             Document document = builder.parse(is); // here is the exception!!

            }
以下是我从服务器得到的xml响应:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
    <GetAllInvoiceStatusesResponse xmlns="http://soap.severa.com/">
        <GetAllInvoiceStatusesResult 
    xmlns:a="http://schemas.datacontract.org/2004/07/Severa.Entities.API"      
     xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:InvoiceStatus>
        <a:Description i:nil="true"/>
        <a:HasInvoiceNumber>false</a:HasInvoiceNumber>
        <a:IsActive>true</a:IsActive>
        <a:IsDefault>true</a:IsDefault>
        <a:IsPaid>false</a:IsPaid>
        <a:IsReadOnly>false</a:IsReadOnly>
        <a:IsSent>false</a:IsSent>
        <a:IsWaitingPayment>false</a:IsWaitingPayment>
        <a:Name>Utkast</a:Name>
        <a:SortOrder>1</a:SortOrder>
        </a:InvoiceStatus>
        <a:InvoiceStatus>
        <a:Description i:nil="true"/>
        <a:HasInvoiceNumber>true</a:HasInvoiceNumber>
        <a:IsActive>true</a:IsActive>
        <a:IsDefault>false</a:IsDefault>
        <a:IsPaid>false</a:IsPaid>
        <a:IsReadOnly>true</a:IsReadOnly>
        <a:IsSent>true</a:IsSent>
        <a:IsWaitingPayment>true</a:IsWaitingPayment>
        <a:Name>Skickad</a:Name>
        <a:SortOrder>2</a:SortOrder>
        </a:InvoiceStatus>
        <a:InvoiceStatus>
        <a:Description i:nil="true"/>
        <a:HasInvoiceNumber>true</a:HasInvoiceNumber>
        <a:IsActive>true</a:IsActive>
        <a:IsDefault>false</a:IsDefault>
        <a:IsPaid>true</a:IsPaid>
        <a:IsReadOnly>true</a:IsReadOnly>
        <a:IsSent>true</a:IsSent>
        <a:IsWaitingPayment>false</a:IsWaitingPayment>
        <a:Name>Betalad</a:Name><a:SortOrder>3</a:SortOrder>
        </a:InvoiceStatus>
        </GetAllInvoiceStatusesResult>
 </GetAllInvoiceStatusesResponse>
</s:Body>

看起来is XML标记未正确关闭,即

SOAPMessage.toString返回如下对象idSOAPMessage@123456.

您想使用SOAPMessage.writeToOutputStream。 或者更好的是,直接转换SOAPMessage,因为它的SOAPPart已经实现了org.w3c.dom