/Tahura Chaudhry/:
I am attempting to creating an XML document and then serialize it into a
Java string. My code looks like:
Document doc= new org.apache.xerces.dom.DocumentImpl();
Element root = doc.createElementNS("http://www.foo.com", "ns1:data");
...
When I print out the xmlstr , I see that the namespace declaration is
missing. The prefix "ns1" is used but not declared. The XML printed is:
<?xml version="1.0" encoding="UTF-8"?>
<ns1:data/>
What do I need to do to get the namespace declaration to appear in the
XML string.
You need to add it:
root.setAttribute("xmlns:ns1", "http://www.foo.com");
or may be:
root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns1",
"http://www.foo.com");
the later being useful only if you plan to manipulate the DOM
furhter and you would rely on the namespaceURI property.
Here's what I read in the DOM Level 2 Core specification regarding
"XML Namespaces"
<http://www.w3.org/TR/DOM-Level-2-Core/core.html#Namespaces-Considerations>:
As far as the DOM is concerned, special attributes used for
declaring XML namespaces are still exposed and can be manipulated
just like any other attribute. However, nodes are permanently bound
to namespace URIs as they get created. Consequently, moving a node
within a document, using the DOM, in no case results in a change of
its namespace prefix or namespace URI. Similarly, creating a node
with a namespace prefix and namespace URI, or changing the namespace
prefix of a node, does not result in any addition, removal, or
modification of any special attributes for declaring the appropriate
XML namespaces. Namespace validation is not enforced; the DOM
application is responsible. In particular, since the mapping between
prefixes and namespace URIs is not enforced, in general, the
resulting document cannot be serialized naively. For example,
applications may have to declare every namespace in use when
serializing a document.
--
Stanimir
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]