没有名称空间的 SOAP header
SOAP header without namespace
我想创建一个 SOAP header。那应该包含一个没有命名空间的元素令牌。如果我不提供命名空间,我将收到“Header 元素必须是 namespace-qualified”错误。
下面是我写的代码。
QName QUsername =
new QName("http://xxxx", "Token");
SOAPHeaderElement soapHeader = soapMessage.getSOAPHeader().addHeaderElement(QUsername);
soapHeader.addTextNode("XXX");
QName header1 = new QName("http://xxxx", "sHeader");
soapMessage.getSOAPHeader().addHeaderElement(header1).addChildElement(soapHeader);
我得到的输出是:
<soap:Header>
<sHeader xmlns="http://xxx">
<Token xmlns="http://xxx">
XXXX
</Token>
</sHeader>
</soap:Header>
预期肥皂 header:
<soap:Header>
<sHeader xmlns="http://xxxx">
<Token>XXXX</Token>
</sHeader>
</soap:Header>
如果将 token 元素附加到 sHeader
元素,它会按预期工作。 Token
默认命名空间(无前缀)继承自 sHeader
parent.
QName QUsername = new QName("http://xxxx", "Token");
SOAPHeaderElement soapHeader;
try {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
//envelope.addNamespaceDeclaration("ex", "http://xxx");
soapHeader = soapMessage.getSOAPHeader().addHeaderElement(QUsername);
QName header1 = new QName("http://xxxx", "sHeader");
SOAPElement head1Child = soapMessage.getSOAPHeader().addHeaderElement(header1).addChildElement(soapHeader);
head1Child.addTextNode("XXX");
soapMessage.writeTo(System.out);
System.out.println();
} catch (SOAPException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
结果(格式化):
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<sHeader xmlns="http://xxxx">
<Token>XXX</Token>
</sHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body/>
</SOAP-ENV:Envelope>
我想创建一个 SOAP header。那应该包含一个没有命名空间的元素令牌。如果我不提供命名空间,我将收到“Header 元素必须是 namespace-qualified”错误。 下面是我写的代码。
QName QUsername =
new QName("http://xxxx", "Token");
SOAPHeaderElement soapHeader = soapMessage.getSOAPHeader().addHeaderElement(QUsername);
soapHeader.addTextNode("XXX");
QName header1 = new QName("http://xxxx", "sHeader");
soapMessage.getSOAPHeader().addHeaderElement(header1).addChildElement(soapHeader);
我得到的输出是:
<soap:Header>
<sHeader xmlns="http://xxx">
<Token xmlns="http://xxx">
XXXX
</Token>
</sHeader>
</soap:Header>
预期肥皂 header:
<soap:Header>
<sHeader xmlns="http://xxxx">
<Token>XXXX</Token>
</sHeader>
</soap:Header>
如果将 token 元素附加到 sHeader
元素,它会按预期工作。 Token
默认命名空间(无前缀)继承自 sHeader
parent.
QName QUsername = new QName("http://xxxx", "Token");
SOAPHeaderElement soapHeader;
try {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
//envelope.addNamespaceDeclaration("ex", "http://xxx");
soapHeader = soapMessage.getSOAPHeader().addHeaderElement(QUsername);
QName header1 = new QName("http://xxxx", "sHeader");
SOAPElement head1Child = soapMessage.getSOAPHeader().addHeaderElement(header1).addChildElement(soapHeader);
head1Child.addTextNode("XXX");
soapMessage.writeTo(System.out);
System.out.println();
} catch (SOAPException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
结果(格式化):
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<sHeader xmlns="http://xxxx">
<Token>XXX</Token>
</sHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body/>
</SOAP-ENV:Envelope>