I want to create a SOAP header. That should contain an Element Token without namespace. If I don't give namespace I'm getting the "Header element must be namespace-qualified" error. Below is the code I wrote.
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);
Output I'm getting is:
<soap:Header>
<sHeader xmlns="http://xxx">
<Token xmlns="http://xxx">
XXXX
</Token>
</sHeader>
</soap:Header>
Expected soap header:
<soap:Header>
<sHeader xmlns="http://xxxx">
<Token>XXXX</Token>
</sHeader>
</soap:Header>
CodePudding user response:
It works as expected if token element is appended to sHeader
element. The Token
default namespace (no prefix) is inherited from 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();
}
Result (formatted):
<?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>