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).
Author by
Admin
Updated on June 28, 2022Comments
-
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 over 10 yearsthanks , 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 over 10 years@Alex please check the reference link there u will find the solution i checked just now
-
Asraful over 10 yearsi 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 over 10 yearsthanks, but it is not about three way communication, I need to know how to add header to it, add namespace to all tags
-
Asraful over 10 yearsif 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 over 10 yearsMaybe use the method addHeader of the SOAPEnvelope object?
-
Admin over 10 yearsI've used that before but accidentally removed it :(
-
Admin over 10 yearsthanks, 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 over 10 yearsUse 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 over 10 yearsI'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 over 10 yearsonce bounty is completed, please remind me to give you a bounty, for your help so far.
-
mwhs over 10 yearsOk, then you have to create the name like this:
soapBody.addChildElement("v9:SomeElement");
(answer updated) -
mwhs over 10 yearsDid you declare the els namespace before?
-
Admin over 10 yearsnot sure what you mean by declaring, I just used the addnamespaceDeclaration for soapbody and updated the question with my latest code
-
mwhs over 10 yearsSomething like this:
soapEnvelope.addNamespaceDeclaration("els", "Namspace-URI");
? -
Admin over 10 yearsyes I have it as you see in the question. soapEnvelope.addNamespaceDeclaration("v9", "URL TO SERVER");
-
mwhs over 10 yearsIn the question I only see "v9".
-
Admin over 10 years
-
Admin over 10 yearsI 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/…