java - SOAPMessage 删除命名空间
问题描述
我正在尝试使用下面的代码使用 Java 创建一个 soapmessage 使用soapmessage 类,创建了soappart、soapenvelope、标题和正文。
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();
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();
envelope.removeNamespaceDeclaration(envelope.getPrefix());
envelope.addNamespaceDeclaration("soapenv", envelope.getNamespaceURI());
envelope.setPrefix("soapenv");
header.setPrefix("soapenv");
body.setPrefix("soapenv");
envelope.addNamespaceDeclaration("", "http://ACORD.org/Standards/Life/2");
QName childName = new QName("TxLife");
SOAPElement txlife = body.addChildElement(childName);
childName = new QName("TxLifeRequest");
SOAPElement txLifeRequest1 = txlife.addChildElement(childName);
childName = new QName("RefGUID");
SOAPElement refGUID = txLifeRequest1.addChildElement(childName);
childName = new QName("TransType");
SOAPElement transType = txLifeRequest1.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();
}
}
}
当我运行代码时,我得到以下信息
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://ACORD.org/Standards/Life/2"><soapenv:Header/><soapenv:Body>
<TxLife xmlns="">
<TxLifeRequest xmlns="http://ACORD.org/Standards/Life/2">
<RefGUID/>
<TransType/></TxLifeRequest>
</TxLife>
</soapenv:Body></soapenv:Envelope>
SOAP msg created
我不希望 xmlnamespace 出现在 TxLifeRequest 标记中我尝试使用下面的代码删除命名空间,但它没有做任何事情
txliferequest1.removeNameSpaceDeclaration("");
感谢帮助
解决方案
推荐阅读
- roslyn-code-analysis - 在 DiagnosticAnalyzer 的操作回调中,如何获取派生 SyntaxNode 的文档或项目?
- java - N个数字的所有可能组合以求和X
- java - Java Spring Boot - 如何使用户能够从命令行切换数据文件?
- r - 从边缘定义一个转移概率矩阵
- html - 无法使图标留在输入内
- python - 基于高级期望行为示例的自动低级测试生成
- javascript - JavaScript:setInterval 函数仅在浏览器空闲时计数?
- javascript - 如何让 style.visibility 使用外部样式表?(只有香草 javascript,没有 jQuery)
- css - 无法将绝对 div 居中对齐
- theano - Theano 导入错误:“AttributeError:模块 'theano' 没有属性 'gof'”