How to remove a namespace from a tag, but leave your prefix?

I can create a SOAP message, but I don't know

  • add prefix only to soapMessage tag (there should be no namespace)

     SOAPConnectionFactory soapConnectionFactory =
                    SOAPConnectionFactory.newInstance();
     SOAPConnection connection =
                    soapConnectionFactory.createConnection();
     SOAPFactory soapFactory =
                    SOAPFactory.newInstance();
    
     MessageFactory factory =
                    MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
    
     SOAPMessage message = factory.createMessage();
     SOAPHeader header = message.getSOAPHeader();
     SOAPPart soapPart = message.getSOAPPart();
     SOAPEnvelope soapEnvelope = soapPart.getEnvelope();                
     SOAPBody body = soapEnvelope.getBody();
    
     soapEnvelope.removeNamespaceDeclaration(soapEnvelope.getPrefix());
     soapEnvelope.setPrefix("soap");
     body.setPrefix("soap");
    
     header.removeNamespaceDeclaration(header.getPrefix());
     header.setPrefix("soap");
    
     soapEnvelope.addNamespaceDeclaration("v9", "URL TO SERVER");
    
     Name bodyName;
     bodyName = soapFactory.createName("SearchHotels");
     SOAPBodyElement getList = body.addBodyElement(bodyName);
     getList.setPrefix("v9");
    
     Name childName = soapFactory.createName("SoapMessage", "v9", "URL TO SERVER");
     SOAPElement HotelListRequest = getList.addChildElement(childName);
    
     HotelListRequest.addChildElement("Hotel", "v9").addTextNode("Hilton");
    

My SOAP Message

   ...
     <v9:SoapMessage xmlns:els="URL TO SERVER">
         ...

What i expect

   ...
      <v9:SoapMessage>
           ...

Update:

If I use the following, it executes the following error:

    SOAPElement HotelListRequest = getList.addChildElement("v9:SoapMessage");

    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.
+4
source share
2 answers

To add a namespace prefix to all tags, you must update the desired prefix (and ultimately the namespace) in each child inserted, otherwise it will inherit the namespace (implicitly) from the parent element.

Try for example:

SOAPBodyElement getList = body.addBodyElement(bodyName, "v9", "http://URL TO SERVER");

or

soapBody.addChildElement("SomeElement", "v9", "http://URL TO SERVER");

or

soapBody.addChildElement("v9:SomeElement");

QName String Name.

SOAP-API/, : redeclare (), ().

+5

SOAP , , , , .net, .

, :

       MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL); 
    SOAPMessage soapMessage = messageFactory.createMessage(); 

    SOAPPart soapPart = soapMessage.getSOAPPart(); 

    SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); 

    SOAPBody soapBody = soapEnvelope.getBody(); 

    soapEnvelope.removeNamespaceDeclaration(soapEnvelope.getPrefix()); 
    soapEnvelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/"); 

    soapEnvelope.setPrefix("soap"); 
    soapBody.setPrefix("soap"); 

    soapEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
    soapEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");   soapMessage.getSOAPHeader().detachNode(); 

    soapMessage.getMimeHeaders().setHeader("SOAPAction", "http://www.example.com/TransactionProcess"); 

.

-1

All Articles