简体   繁体   English

为什么我得到org.xml.sax.SAXParseException; 序言中不能有内容?

[英]Why am I getting org.xml.sax.SAXParseException; Content is not allowed in prolog?

I am trying to parse an xml file but it continuously goes into exception. 我正在尝试解析一个xml文件,但是它不断陷入异常。 I have looked here in for some solution as many had almost same problem as me but it was in vain. 我在这里寻找了一些解决方案,因为许多解决方案都和我有几乎相同的问题,但徒劳无功。 The request and response seems to be correct and I have checked my xml and I am not able to find anything wrong in it. 请求和响应似乎是正确的,并且我已经检查了xml,但找不到任何错误。

Here is my Java code: 这是我的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!!

            }

Here is the xml response I am getting from the server: 这是我从服务器获取的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>

看起来XML标记未正确关闭,即<s:envelope>

SOAPMessage.toString() returns the object id like "SOAPMessage@123456". SOAPMessage.toString()返回对象ID,例如“ SOAPMessage @ 123456”。

You want to use SOAPMessage.writeTo(OutputStream). 您要使用SOAPMessage.writeTo(OutputStream)。 Or better yet, directly transform the SOAPMessage as its SOAPParts already implement org.w3c.dom. 或者更好的是,直接转换SOAPMessage,因为其SOAPParts已经实现了org.w3c.dom。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.xml.sax.SAXParseException; 序言中不能有内容 - org.xml.sax.SAXParseException; Content is not allowed in prolog org.xml.sax.SAXParseException:序言中不允许内容 - org.xml.sax.SAXParseException: Content is not allowed in prolog org.xml.sax.SAXParseException:序言(JSON)中不允许内容 - org.xml.sax.SAXParseException: Content is not allowed in prolog (JSON) org.xml.sax.SAXParseException:prolog 问题中不允许内容 - org.xml.sax.SAXParseException: Content is not allowed in prolog Problems org.xml.sax.SAXParseException:序言中不允许内容 - org.xml.sax.SAXParseException: Content is not allowed in prolog 为什么我得到这个,org.xml.sax.SAXParseException异常? - Why I'm getting this, org.xml.sax.SAXParseException, exception? org.xml.sax.SAXParseException:在序言中不允许引用 - org.xml.sax.SAXParseException: Reference is not allowed in prolog 当针对XSD验证XML时,为什么会出现org.xml.sax.SAXParseException? - Why am I getting org.xml.sax.SAXParseException when validate XML against XSD? xml保存并解析:org.xml.sax.SAXParseException:序言中不允许内容 - xml save and parse : org.xml.sax.SAXParseException: Content is not allowed in prolog javax.xml.bind.UnmarshalException - 带有链接异常:[org.xml.sax.SAXParseException:序言中不允许内容。] - javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException: Content is not allowed in prolog.]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM