Hi,
I am using the latest Xerces API from Apache. I create a document and then
create some elements and set some attributes by using
Document.createElementNS() method and Element.setAttributeNS(). Now when I
try to access an attribute value from one of the element using
Element.getAttributeNS() method I successfully got the value. Then I saved
the document object to file. Then I read the file and create the document
object using DOMParser class. Now again I try to access the same attribute
value from the same element using Element.getAttributeNS() method but this
time I am unable to get the value of the attribute. On the other hand if I
try to access the attribute value using Element.getAttribute() it
successfully returns the value. It shows Element.getAttributeNS() is not
working in the same way after creating the document from file.
I am writing my code below:
/* creating document */
String str_xmlNS = "http://www.w3.org/2000/xmlns/";
String str_myNS = "http://www.ascertia.com/xml/";
Document obj_doc = XMLUtil.createDocument();
Element obj_root = obj_doc.createElementNS(str_myNS,"myns:root");
obj_root.setAttributeNS(str_xmlNS,"xmlns:myns",str_myNS);
obj_root.setAttributeNS(str_myNS,"attribute","value");
Element obj_element = obj_doc.createElementNS(str_myNS, "myns:element");
obj_root.appendChild(obj_element);
obj_doc.appendChild(obj_root);
/* printing and saving to file */
System.out.println("--- Before saving to file ---");
System.out.println("Namespace URI is :
"+obj_doc.getDocumentElement().getNamespaceURI());
System.out.println("NS Attribute value is :
"+obj_doc.getDocumentElement().getAttributeNS(str_myNS, "attribute"));
XMLWriter writer = new XMLWriter();
writer.setOutput(new FileOutputStream("c:/xml.xml"), "utf-8");
writer.write(obj_doc);
/*output in file is below*/
/*
<?xml version="1.0" encoding="UTF-8"?>
<myns:root attribute="value"
xmlns:myns="http://ww.ascertia.com/xml/">
<myns:element></myns:element>
</myns:root>
*/
/*reading from file and printing - but it doesn't work*/
System.out.println("--- After saving to file ---");
org.apache.xerces.parsers.DOMParser obj_parser = new
org.apache.xerces.parsers.DOMParser();
obj_parser.parse("c:/xml.xml");
Document obj_doc1 = obj_parser.getDocument();
System.out.println("Namespace URI is :
"+obj_doc1.getDocumentElement().getNamespaceURI());
System.out.println("NS Attribute value is :
"+obj_doc1.getDocumentElement().getAttributeNS(str_myNS, "attribute"));
/*it works*/
System.out.println("--- Following works ---");
System.out.println("Attribute value is :
"+obj_doc1.getDocumentElement().getAttribute("attribute"));
Below is the out put of the program:
--- Before saving to file ---
Namespace URI is : http://ww.ascertia.com/xml/
NS Attribute value is : value
--- After saving to file ---
Namespace URI is : http://ww.ascertia.com/xml/
NS Attribute value is :
--- Following works ---
Attribute value is : value
Please tell me what is going wrong in the above program.
Thanx & regards,
Yasir Khan
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]