I have created a SOAPHandler at client side to log outgoing request to server & modify some elements using JAX-WS.
After I modified elements, I save the message:
try {
// modifying elements
SOAPBody body = soapMsg.getSOAPBody();
NodeList blst = body.getElementsByTagName("ns6:exportNsiItemRequest");
Node itm = blst.item(0);
Node itm2 = itm.getFirstChild();
Document doc = body.getOwnerDocument();
doc.adoptNode(nd);
itm.insertBefore(nd, itm2);
soapMsg.saveChanges();
log.info("XML saved!");
} catch (SOAPException e1) {
log.info("XML DOESN'T saved!");
e1.printStackTrace();
}
then I print the message:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
msg.writeTo(baos);
System.out.println(baos.toString(getMessageEncoding(msg)));
This is a part of printed message:
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" Id="xmldsig-4cf24b6d-5c1a-4756-9657-3ba87c3af164">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
I use the Wireshark to see context of the actual soap message which goes to server.
And WHAT I SEE?? It is:
<ds:Signature
xmlns=""
Id="xmldsig-4cf24b6d-5c1a-4756-9657-3ba87c3af164"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod
Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
Why something modified my message and insert empty xlnms="" tag???
The inserted tag is related to XML Namespaces. They were defined by the World Wide Web Consortium in 1999 (you can check the document here, and also its more recent version). As mentioned in those documents:
XML namespaces provide a simple method for qualifying element and attribute names used in Extensible Markup Language documents by associating them with namespaces identified by URI references.
Doing some research I found this reference that talks about the xmlns
tag, extracting from its contents:
The prefix
xmlns:
was specified as a syntactic device for declaring namespaces, but was not itself associated with any namespace name by the Jan 1999 namespaces specification ...Note that you must use the reserved prefix
xmlns:
when declaring namespaces ...The attribute name
xmlns
, which is used to declare the default namespace, is also hereby associated with this namespace name...
As I mentioned in a comment, I believe there is more code you could share that will probably shed more light on why this is happening to you, but as we can see from the reference the empty xmlns
tag is being added probably as a required default value (that is, there is no default namespace prefix for the URIs on your document).
To explain it further, lets look at that other namespace you declared in your document (where you correctly used the xmlns:
prefix as the reference states):
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
Here, the namespace prefix (like an alias) is ds
and the namespace URI is http://www.w3.org/2000/09/xmldsig#
. This means that in your document an element like <ds:foo />
will be equivalent to having the identifier <http://www.w3.org/2000/09/xmldsig#:foo />
when parsing the document.
If you want to know more you can take a look a this great question and answers that explains more about namespaces and gives some useful external references.