java - Soapmessage 添加命名空间
问题描述
我正在尝试使用 javax.xml.soap.* 创建一个具有两个命名空间的肥皂消息。但是正在创建的消息不会添加第二个命名空间。我不确定为什么它不能识别第二个 addnamespacedeclaration 语句。
感谢帮助
package testsoapmsg;
import java.io.FileOutputStream;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
public class CreateeSoapMessage {
public static void main(String[] args) {
try{
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage soapMsg = factory.createMessage();
SOAPPart part = soapMsg.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
envelope.addNamespaceDeclaration("xmlns", "http://schemas.xmlsoap.org/soap/envelope/");
envelope.addNamespaceDeclaration("xmlns", "http://ACORD.org/Standards/Life/2");
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();
QName childname = new QName("TxLife");
SOAPElement txlife = body.addChildElement(childname);
soapMsg.writeTo(System.out);
FileOutputStream fOut = new FileOutputStream("SoapMessage.xml");
soapMsg.writeTo(fOut);
System.out.println();
System.out.println("SOAP msg created");
}catch(Exception e){
e.printStackTrace();
}
}
}
我得到的输出是
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><TxLife/></SOAP-ENV:Body></SOAP-ENV:Envelope>
SOAP msg created
解决方案
推荐阅读
- android - 使用junit在android的框架中测试业务逻辑
- azure - 如何从 azure 函数的 appinsight 中排除异常?
- django - Django 2.2 - 如何表达两个字段之间的约束
- kubernetes - 使用 GKE 在 StackDriver 中拆分日志条目
- ios - Realm 应用程序可以在设备上编译,但不能在 Xcode 12 上的模拟器上编译
- glusterfs - GlusterFS 地理复制:未找到 gsync peer_gsec_create 命令
- spring-cloud-dataflow - 如何更新 SCDF 使用的 Hibernate 版本?
- python - 在 Heroku 上上传网站后,静态(CSS 和 JS)文件无法正常工作
- ruby - http.post 请求未收到任何数据
- azure - 源链接服务不应将“服务主体”作为身份验证方法