我想添加属性到肥皂元素像

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

谁能帮我!

最佳答案

以下代码将为您带来预期的结果。

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>

09-18 23:47