我想将属性添加到soap元素,如
<SdcPDU xmlns="urn:sdcf:sdc:xsd:faa.2.0">
<Revision>2.0.2</Revision>
</SdcPDU>
java代码
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();
但输出消息看起来像
<SdcPDU xmlns="">
<Revision xmlns="urn:sdcf:sdc:xsd:faa.2.0">2.0.2</Revision>
</SdcPDU>
任何人都可以帮助我!
答案 0 :(得分:5)
以下代码可以为您提供预期的结果。
SOAPElement sdcPDU = soapBody.addChildElement("SdcPDU","","urn:sdcf:sdc:xsd:faa.2.0");
SOAPElement revision = sdcPDU.addChildElement("Revision");
revision.addTextNode("2.0.2");
响应:
<SOAP-ENV:Body> <SdcPDU xmlns="urn:sdcf:sdc:xsd:faa.2.0"> <Revision>2.0.2</Revision> </SdcPDU> </SOAP-ENV:Body>