Hello,
My sample code below is producing the following XML
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<Header1 attr1="value1" attr2="value2"
xmlns="http://mydomain.com/header1"/>
<Header2 xmlns="http://mydomain.com/header2">
<ns1:child1 xmlns:ns1="http://mydomain.com/child1">
<ns1:child2>Value for Child2</ns1:child2>
<child3 xmlns="">Value for Child3</child3>
</ns1:child1>
</Header2>
</soapenv:Header>
<soapenv:Body>
................
</soapenv:Body>
</soapenv:Envelope>
private void createSOAPHeader(boolean test) throws Exception {
String property = null;
// Create the SOAP envelope
SOAPPart soapPart = message.getSOAPPart();
envelope = soapPart.getEnvelope();
// Create the SOAP Header within the envelope
SOAPHeader header = envelope.getHeader();
// Create the Header1 Element and add it
Name header1 = envelope.createName("Header1", "",
"http://mydomain.com/header1");
SOAPHeaderElement headerElement = header.addHeaderElement(header1);
// Add some attributes
Name attr1 = envelope.createName("attr1");
headerElement.addAttribute(attr1, "value1");
Name attr2 = envelope.createName("attr2");
headerElement.addAttribute(attr2, "value2");
// Create the Header2 Element and add it
Name header2 = envelope.createName("Header2", "",
"http://mydomain.com/header2");
headerElement = header.addHeaderElement(header2);
// Create some child Elements for Header2
Name name = envelope.createName("child1", "child1_ns",
"http://mydomain.com/child1");
SOAPElement child1 = headerElement.addChildElement(name);
// Create child2 element
name = envelope.createName("child2", "child1_ns",
"http://mydomain.com/child1");
SOAPElement child2 = child1.addChildElement(name);
child2.addTextNode("Value for Child2");
// Create child3 element
name = envelope.createName("child3");
SOAPElement child3 = child1.addChildElement(name);
child3.addTextNode("Value for Child3");
}
Questions,
1) The namespace prefix is "ns1", I would like it to be "child1_ns" -
how can I control this
2) How can I omit the namespace prefix before an Element name for example
how can I get encode
<ns1:child1 xmlns:ns1="http://mydomain.com/child1">
to be
<child1 xmlns:ns1="http://mydomain.com/child1">
3) How can I produce the following output
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<Header1 attr1="value1" attr2="value2"
xmlns="http://mydomain.com/header1"/>
<Header2 xmlns="http://mydomain.com/header2">
<child1 xmlns:ns_child1="http://mydomain.com/child1">
<ns_child1:child2>Value for Child2</ns_child1:child2>
<child3 xmlns="">Value for Child3</child3>
</ns_child1:child1>
</Header2>
</soapenv:Header>
<soapenv:Body>
........................
</soapenv:Body>
</soapenv:Envelope>
- Re: Controlling namespace prefixes in SAAJ Jack Byrne
- Re: Controlling namespace prefixes in SAAJ Scott Nichol
- Controlling namespace prefixes in SAAJ Jack Byrne