如何将前缀应用于SOAP消息

时间:2017-08-05 16:05:14

标签: java xml web-services dom soap

在构造SOAP消息时,使用ext:msg_LocationDetailsQName

javax.xml.soap.Name元素的子元素添加前缀时遇到问题

我正在使用javax.xml.soap.SOAPFactory

当我尝试使用QName mag = new QName("","Messages","ext");时 (新的QName(namespaceURI,localPart,prefix))

Exception in thread "main" org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.
    at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.setName(ElementNSImpl.java:149)
    at com.sun.org.apache.xerces.internal.dom.ElementNSImpl.<init>(ElementNSImpl.java:80)
    at com.su

它给出了上述例外。

SOAPBody soapBody = soapEnvelope.getBody();
    soapBody = soapMessage.getSOAPBody();
    soapBody.setPrefix("soapenv");
    // To add some element
    SOAPFactory soapFactory = SOAPFactory.newInstance();

    Name bodyName = soapFactory.createName("msg_LocationDetails");
    SOAPBodyElement locationDetails = soapBody.addBodyElement(bodyName);
    locationDetails.setPrefix("ext");

    QName mag = new QName("","Messages","ext");
    SOAPElement messages = locationDetails.addChildElement(mag);
    //SOAPElement messages = locationDetails.addChildElement("Messages");
    //messages.addNamespaceDeclaration("fir", "");
    //SOAPElement message = messages.addChildElement("message");

以下是SOAP消息

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ext="http://extdomain.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ext:msg_LocationDetails>
        <ext:Messages>
            <ext:Message>
               <ext:Location>GSA</ext:Location>
               <ext:LocCode>Unit</ext:LocCode>
               <ext:LocID>00004</ext:LocID>
               <ext:SystemID>TSKSLSLSJJ</ext:SystemID>
               <ext:Status>
                  <ext:ID>4</ext:ID>
                  <ext:Name>PENDING</ext:Name>            
               </ext:Status>
             </ext:Message>  
        </ext:Messages>
      </ext:msg_LocationDetails>
    </soapenv:Header/>
 </soapenv:Body>

0 个答案:

没有答案