如何在java中将属性添加到Soap元素中

时间:2014-09-26 15:58:27

标签: java soap

我想将属性添加到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>

任何人都可以帮助我!

1 个答案:

答案 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>