Java SOAPMessage删除命名空间

Java SOAPMessage删除命名空间,java,soap,Java,Soap,我正在尝试使用下面的代码使用Java创建一个soapmessage 使用soapmessage类,创建了soappart、soapenvelope、标头和正文 package testsoapmsg; import java.io.FileOutputStream; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import jav

我正在尝试使用下面的代码使用Java创建一个soapmessage 使用soapmessage类,创建了soappart、soapenvelope、标头和正文

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();

        
            SOAPHeader header = envelope.getHeader();
            SOAPBody body = envelope.getBody();
            envelope.removeNamespaceDeclaration(envelope.getPrefix());
            envelope.addNamespaceDeclaration("soapenv", envelope.getNamespaceURI());
            envelope.setPrefix("soapenv");
            header.setPrefix("soapenv");
            body.setPrefix("soapenv");
            
            envelope.addNamespaceDeclaration("", "http://ACORD.org/Standards/Life/2");
        
            
            QName childName = new QName("TxLife");
            SOAPElement txlife = body.addChildElement(childName);
            
            childName = new QName("TxLifeRequest");
            SOAPElement txLifeRequest1 = txlife.addChildElement(childName);
            
            childName = new QName("RefGUID");
            SOAPElement refGUID = txLifeRequest1.addChildElement(childName);
            
            childName = new QName("TransType");
            SOAPElement transType = txLifeRequest1.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();
              
                        }
              


    }

}
当我运行代码时,我得到以下信息

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://ACORD.org/Standards/Life/2"><soapenv:Header/><soapenv:Body>
<TxLife xmlns="">
<TxLifeRequest xmlns="http://ACORD.org/Standards/Life/2">
<RefGUID/>
<TransType/></TxLifeRequest>
</TxLife>
</soapenv:Body></soapenv:Envelope>
SOAP msg created
谢谢你的帮助

txliferequest1.removeNameSpaceDeclaration("");