How to add Soap Element attribute in java
I want to add an attribute to a soap element like
<SdcPDU xmlns="urn:sdcf:sdc:xsd:faa.2.0">
<Revision>2.0.2</Revision>
</SdcPDU>
java code
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
...
SOAPElement sdcPDU = soapBody.addChildElement("SdcPDU");
Name sdcPDUPduName = envelope.createName("xmlns");
sdcPDU.addAttribute(sdcPDUPduName, "urn:sdcf:sdc:xsd:faa.2.0");
SOAPElement revision = sdcPDU.addChildElement("Revision");
revision.addTextNode(2.0.2);
...
...
soapMessage.saveChanges();
but the output message looks like
<SdcPDU xmlns="">
<Revision xmlns="urn:sdcf:sdc:xsd:faa.2.0">2.0.2</Revision>
</SdcPDU>
can someone help me!
+3
source to share
1 answer
The following code should give you what you expect.
SOAPElement sdcPDU = soapBody.addChildElement("SdcPDU","","urn:sdcf:sdc:xsd:faa.2.0");
SOAPElement revision = sdcPDU.addChildElement("Revision");
revision.addTextNode("2.0.2");
Answer:
<SOAP-ENV:Body>
<SdcPDU xmlns="urn:sdcf:sdc:xsd:faa.2.0">
<Revision>2.0.2</Revision>
</SdcPDU>
</SOAP-ENV:Body>
+4
source to share