How to remove namespace from a tag but leave its prefix?

10,165

To add the namespace prefix to all the tags you have to redeclare the desired prefix (and eventually the namespace) on every inserted child, otherwise it will inherit the namespace (implicitely) from the parent element.

Try for instance:

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

or

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

or

soapBody.addChildElement("v9:SomeElement");

Sometimes you may have to use a QName object instead of just a String or a Name.

It pretty much depends on the SOAP-API/Implementation you use, but the principle is the same everywhere: either redeclare (explicit) or inherit (implicit).

Share:
10,165
Admin
Author by

Admin

Updated on June 28, 2022

Comments

  • Admin
    Admin almost 2 years

    I am able to generate a SOAP message but I do not know

    • add prefix only to soapMessage tag (should not have 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 runs into 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.
    
  • Admin
    Admin over 10 years
    thanks , but could not figure out how to add the prefix to the tags, the one that you mention does not work, also now sure how to remove schemas..... namespace and also remove the namespace of searchHotels tag
  • Asraful
    Asraful over 10 years
    @Alex please check the reference link there u will find the solution i checked just now
  • Asraful
    Asraful over 10 years
    i also face some integration problem while trying to write client for different payment gateway , where three way communication was involved - our system, gateway , and another thirdparty system of client -- in three case we had to sync the transaction information with retaining security and integrity , good luck
  • Admin
    Admin over 10 years
    thanks, but it is not about three way communication, I need to know how to add header to it, add namespace to all tags
  • Asraful
    Asraful over 10 years
    if u want to build <xs:tag> in body section with prefix and namespace , you can use manual process using StringBuilder -- though may be this is a temporary solution as i think ,
  • mwhs
    mwhs over 10 years
    Maybe use the method addHeader of the SOAPEnvelope object?
  • Admin
    Admin over 10 years
    I've used that before but accidentally removed it :(
  • Admin
    Admin over 10 years
    thanks, I could change it, the only thing that I need is to add prefix to the following SoapMessage tag I would appreciate it if you could help me, Name bodyName; bodyName = soapFactory.createName("SearchHotels"); SOAPBodyElement getList = body.addBodyElement(bodyName); getList.setPrefix("v9"); Name childName = soapFactory.createName("SoapMessage"); SOAPElement HotelListRequest = getList.addChildElement(childName);
  • mwhs
    mwhs over 10 years
    Use the version of createName which offers you to also pass a NS-prefix and the corresponding NS. See this link: docs.oracle.com/javaee/1.4/api/javax/xml/soap/…
  • Admin
    Admin over 10 years
    I've found that as well but the problem is that I do not want it to add namespace url, just need to have the prefix
  • Admin
    Admin over 10 years
    once bounty is completed, please remind me to give you a bounty, for your help so far.
  • mwhs
    mwhs over 10 years
    Ok, then you have to create the name like this: soapBody.addChildElement("v9:SomeElement"); (answer updated)
  • mwhs
    mwhs over 10 years
    Did you declare the els namespace before?
  • Admin
    Admin over 10 years
    not sure what you mean by declaring, I just used the addnamespaceDeclaration for soapbody and updated the question with my latest code
  • mwhs
    mwhs over 10 years
    Something like this: soapEnvelope.addNamespaceDeclaration("els", "Namspace-URI");?
  • Admin
    Admin over 10 years
    yes I have it as you see in the question. soapEnvelope.addNamespaceDeclaration("v9", "URL TO SERVER");
  • mwhs
    mwhs over 10 years
    In the question I only see "v9".
  • Admin
    Admin over 10 years
  • Admin
    Admin over 10 years
    I need to wait 23 hours to award the bounty :D I have a new question please have a look at it thanks stackoverflow.com/questions/19953829/…