473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

How to declare namespace prefix in Java?

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:doSpellingSuggestion
xmlns:ns1='urn:GoogleSearch'

SOAP-ENV:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>
<key xsi:type='xsd:string'>mykey</key>
<phrase xsi:type='xsd:string'>word2check</phrase>
</ns1:doSpellingSuggestion>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Currently, my program is as follows:

....
Document doc =
impl.createDocument("http://schemas.xmlsoap.org/soap/envelope/",
"SOAP-ENV:Envelope",
null);
Element root = doc.getDocumentElement();
/* I doubt this is the right way to declare namespace prefix */
root.setAttribute("xmlns:xsi",
"http://www.w3.org/1999/XMLSchema-instance");
root.setAttribute("xmlns:xsd", "http://www.w3.org/1999/XMLSchema");

Element body =
doc.createElementNS("http://schemas.xmlsoap.org/soap/envelope/",
"SOAP-ENV:Body");
root.appendChild(body);

Element doSpellingSuggestion = doc.createElementNS("urn:GoogleSearch",
"ns1:doSpellingSuggestion");
doSpellingSuggestion.setAttributeNS("http://schemas.xmlsoap.org/soap/envelope/",
"SOAP-ENV:encodingStyle",
"http://schemas.xmlsoap.org/soap/encoding/");
body.appendChild(doSpellingSuggestion);

Element key = doc.createElement("key");
/ * well, i can use setAttribute() to get rid of the extra namespace in
the output, but is there a better way?*/
key.setAttributeNS("http://www.w3.org/1999/XMLSchema-instance",
"xsi:type", "xsd:string");
doSpellingSuggestion.appendChild(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:doSpellingSuggestion
xmlns:ns1="urn:GoogleSearch"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><key
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:type="xsd:string">mykey</key><phrase
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:type="xsd:string">word2check</phrase></ns1:doSpellingSuggestion></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!

Mar 14 '06 #1
2 18700
If you're working with the DOM, there are two answers to this.

One is that, in many cases, it isn't strictly necessary to have an
actual namespace declaration. Use the proper createElementNS and
createAttributeNS directives, and the code which serializes the DOM out
to XML syntax should be able to create delarations automatically. See
the DOM level 3 working draft for a description of the algorithms
typically used to do namespace reconcilliation in the DOM.

On the other hand, some programs want an explicit namespace declaration
in the DOM for internal purposes, and creating one at a higher level of
the document will avoid potentially having to generate one at multiple
places lower down. So the DOM permits you to do this; the trick is to
use createAttributeNS or setAttributeNS to create an attribute in the
"namespace for namespaces" with the prefix xmlns:, the localname set to
the prefix you want to define, and the value being the URI you want to
bind that prefix to. Thus, uour example should be coded as:

root.setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns:xsi",
"http://www.w3.org/1999/XMLSchema-instance");

Important Note: If you're working with namespaces, you ABSOLUTELY MUST
NOT use the setAttribute, createAttribute, or createElement calls.
****ONLY**** the setAttributeNS, createAttributeNS, and createElementNS
versions are compatable with namespace-aware processing. (There are good
but ugly reasons why we couldn't deprecate the older calls, but they
should be considered obsolete except for backward compatability with
code that is intended to work with a Level 1 DOM.)

See DOM Level 2's description of how to work with namespaces; a copy can
be found at
http://www.w3.org/TR/2000/REC-DOM-Le...Considerations
How do I get rid of the superfluous namespace thingy in the <key> and
<phrase> element? Thanks a lot!


If the above changes don't resolve this for you, it's possible the
serializer you're using is broken...

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 15 '06 #2
Thanks Joe. You rock!!!

Mar 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Divick | last post by:
Hi all, does any one know what is the right way to forward declare classes within namespaces. Though I have been using the syntax as follows but it doesn't sound good to me. namespace...
4
by: Krishna Tulasi via .NET 247 | last post by:
Hi, I am having trouble with creation of XML programmatically using .NET. Specifically Im trying to create an element which looks like below and insert into an existing xml doc: <Worksheet...
5
by: pneumoconi | last post by:
I've read a lot of posts on this subject each with a slightly different issue and from what I gather my code is fine. But its not. I'm trying to pull out the SQL query from a SQL report document...
5
by: David Thielen | last post by:
Hi; I set up my xml as follows: XmlDocument xml = new XmlDocument(); xml.Load(File.Open("data.xml", FileMode.Open, FileAccess.Read)); XmlNamespaceManager context = new...
4
by: izhar.wallach | last post by:
Hi all, I've noticed an odd behavior of the linker while trying to compile the code below. The linking did not fail even though variable a1 (A2.cpp) was defined as extern in namespace A2 while...
2
by: VanOrton | last post by:
Hi all, When XercesDOMParser parses an XML document with a Schema instance, by default it adds all missing default and fixed attributes. I have the impression though that it misses the...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
2
by: scottpet | last post by:
Hi, I want to add a namespace prefix to the root node of an object I am serializing to XML. I have been reading though this article:...
2
by: Philipp | last post by:
Hello, I am not able to read the namespace for my elements correctly. Could somebody point me to my error. Thank you. Philipp Compilable example: import java.io.ByteArrayInputStream; import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.