首页 > 解决方案 > Soapmessage 添加命名空间

问题描述

我正在尝试使用 javax.xml.soap.* 创建一个具有两个命名空间的肥皂消息。但是正在创建的消息不会添加第二个命名空间。我不确定为什么它不能识别第二个 addnamespacedeclaration 语句。

感谢帮助


package testsoapmsg;

    import java.io.FileOutputStream;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;

     

public class CreateeSoapMessage {
    public static void main(String[] args) {
        try{
            MessageFactory factory = MessageFactory.newInstance();
            SOAPMessage soapMsg = factory.createMessage();
            SOAPPart part = soapMsg.getSOAPPart();
            SOAPEnvelope envelope = part.getEnvelope();
            envelope.addNamespaceDeclaration("xmlns", "http://schemas.xmlsoap.org/soap/envelope/");
            envelope.addNamespaceDeclaration("xmlns", "http://ACORD.org/Standards/Life/2");
            SOAPHeader header = envelope.getHeader();
            SOAPBody body = envelope.getBody();
            QName childname = new QName("TxLife");
            SOAPElement txlife = body.addChildElement(childname);
            
              soapMsg.writeTo(System.out);
                FileOutputStream fOut = new FileOutputStream("SoapMessage.xml");
                soapMsg.writeTo(fOut);
                System.out.println();
                System.out.println("SOAP msg created");  
                
          }catch(Exception e){
              
                            e.printStackTrace();
              
                        }
              


    }

}

我得到的输出是

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><TxLife/></SOAP-ENV:Body></SOAP-ENV:Envelope>
SOAP msg created

标签: javasoap

解决方案


推荐阅读