You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Yasir Khan <ya...@ascertia.com> on 2003/09/19 08:17:53 UTC
Problem in using Document.createElementNS() method
Hi,
I am using Xerces 2.5.0 to create the following document:
<?xml version="1.0" encoding="UTF-8"?>
<myns:root xmlns:myns="http://www.ascertia.com/xml/">
<myns:element1>text1</myns:element1>
</myns:root>
I m writting the following code to produce above document
Document doc = new DocumentImpl();
Element root = doc.createElementNS("http://www.ascertia.com/xml/",
"myns:root");
Element element1 = doc.createElement("myns:element1");
element1.appendChild(doc.createTextNode("text1"));
root.appendChild(element1);
doc.appendChild(root);
Writer writer = new Writer(); /*Writer is the class provided in
Xerces DOM Samples*/
try{
writer.setOutput(System.out, "UTF-8");
writer.write(doc);
}
catch (Exception ex) {
ex.printStackTrace();
}
The output of the above code is:
<?xml version="1.0" encoding="UTF-8"?>
<myns:root>
<element1>text1</element1>
</myns:root>
The namespace declation i.e. xmlns:myns="http://www.ascertia.com/xml/" is
missing in the output.
I couldn't figure out that what is wrong in my code. Please tell me where I
am doing wrong and what is the right way to create the above mentioned
target document. Thanx
Regards,
Yasir Khan
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: Problem in using Document.createElementNS() method
Posted by Elena Litani <el...@ca.ibm.com>.
Yasir Khan wrote:
> Writer writer = new Writer(); /*Writer is the class provided in
> Xerces DOM Samples*/
By definition, createElementNS [1] method only creates an element and
never creates a corresponding namespace declaration. Given that neither
Writer nor org.apache.xml.serialize.XMLSerializer by default add missing
namespaces to the DOM tree, you don't see the namespace declaration in
the output.
If you want namespace fixup to take place during serialization, i.e. for
serializer to add missing namespace declarations, use DOM L3 API [2].
One more comment, _never_ mix DOM Level 1 and DOM Level 2 nodes (created
with NS suffix), as you do in your code:
> Element root = doc.createElementNS("http://www.ascertia.com/xml/",
> "myns:root");
> Element element1 = doc.createElement("myns:element1");
That may lead to unpredictable results [3].
[1]
http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030609/core.html#ID-DocCrElNS
[2] http://xml.apache.org/xerces2-j/faq-dom.html#faq-3
[3]
http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030609/core.html#Namespaces-Considerations
Thank you,
--
Elena Litani / IBM Toronto
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org
Re: Problem in using Document.createElementNS() method
Posted by Ray Grimmond <ra...@qwan.com>.
Yasir,
After the following statement
Element root =
doc.createElementNS("http://www.ascertia.com/xml/","myns:root");
insert
root.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:myns",
"http://www.ascertia.com/xml/" );
Good luck
Ray Grimmond
"Swing XMLEditorKit for Java" http://www.qwan.com/XML/XMLEditor.html
Threshold Computer Systems,Inc.
4180 Treat Blvd, Suite R,
Concord, CA 94518, USA
(925) 687-8415
Yasir Khan wrote:
> Hi,
>
> I am using Xerces 2.5.0 to create the following document:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <myns:root xmlns:myns="http://www.ascertia.com/xml/">
> <myns:element1>text1</myns:element1>
> </myns:root>
>
> I m writting the following code to produce above document
>
> Document doc = new DocumentImpl();
> Element root = doc.createElementNS("http://www.ascertia.com/xml/",
> "myns:root");
> Element element1 = doc.createElement("myns:element1");
> element1.appendChild(doc.createTextNode("text1"));
> root.appendChild(element1);
> doc.appendChild(root);
>
> Writer writer = new Writer(); /*Writer is the class provided in
> Xerces DOM Samples*/
> try{
> writer.setOutput(System.out, "UTF-8");
> writer.write(doc);
> }
> catch (Exception ex) {
> ex.printStackTrace();
> }
>
> The output of the above code is:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <myns:root>
> <element1>text1</element1>
> </myns:root>
>
> The namespace declation i.e. xmlns:myns="http://www.ascertia.com/xml/" is
> missing in the output.
>
> I couldn't figure out that what is wrong in my code. Please tell me where I
> am doing wrong and what is the right way to create the above mentioned
> target document. Thanx
>
> Regards,
>
> Yasir Khan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org