I want to declare namespace prefix in the Envelope element (i.e.
xmlns:xsi and xmlns:xsd) so that the document will be serialized to
look like the following. However, I don't know the right way to do
that in Java.
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV='http://schemas.xmlsoap .org/soap/envelope/'
xmlns:xsi='http ://www.w3.org/1999/XMLSchema-instance'
xmlns:xsd='http ://www.w3.org/1999/XMLSchema'>
<SOAP-ENV:Body>
<ns1:doSpelling Suggestion
xmlns:ns1='urn: GoogleSearch'
SOAP-ENV:encodingSty le='http://schemas.xmlsoap .org/soap/encoding/'>
<key xsi:type='xsd:s tring'>mykey</key>
<phrase xsi:type='xsd:s tring'>word2che ck</phrase>
</ns1:doSpellingS uggestion>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Currently, my program is as follows:
....
Document doc =
impl.createDocu ment("http://schemas.xmlsoap .org/soap/envelope/",
"SOAP-ENV:Envelope",
null);
Element root = doc.getDocument Element();
/* I doubt this is the right way to declare namespace prefix */
root.setAttribu te("xmlns:xsi" ,
"http://www.w3.org/1999/XMLSchema-instance");
root.setAttribu te("xmlns:xsd" , "http://www.w3.org/1999/XMLSchema");
Element body =
doc.createEleme ntNS("http://schemas.xmlsoap .org/soap/envelope/",
"SOAP-ENV:Body");
root.appendChil d(body);
Element doSpellingSugge stion = doc.createEleme ntNS("urn:Googl eSearch",
"ns1:doSpelling Suggestion");
doSpellingSugge stion.setAttrib uteNS("http://schemas.xmlsoap .org/soap/envelope/",
"SOAP-ENV:encodingSty le",
"http://schemas.xmlsoap .org/soap/encoding/");
body.appendChil d(doSpellingSug gestion);
Element key = doc.createEleme nt("key");
/ * well, i can use setAttribute() to get rid of the extra namespace in
the output, but is there a better way?*/
key.setAttribut eNS("http://www.w3.org/1999/XMLSchema-instance",
"xsi:type", "xsd:string ");
doSpellingSugge stion.appendChi ld(key)
....
And the output looks like
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap .org/soap/envelope/"
xmlns:xsd="http ://www.w3.org/1999/XMLSchema"
xmlns:xsi="http ://www.w3.org/1999/XMLSchema-instance"><SOAP-ENV:Body><ns1:d oSpellingSugges tion
xmlns:ns1="urn: GoogleSearch"
SOAP-ENV:encodingSty le="http://schemas.xmlsoap .org/soap/encoding/"><key
xmlns:xsi="http ://www.w3.org/1999/XMLSchema-instance"
xsi:type="xsd:s tring">mykey</key><phrase
xmlns:xsi="http ://www.w3.org/1999/XMLSchema-instance"
xsi:type="xsd:s tring">word2che ck</phrase></ns1:doSpellingS uggestion></SOAP-ENV:Body></SOAP-ENV:Envelope>
How do I get rid of the superfluous namespace thingy in the <key> and
<phrase> element? Thanks a lot!