Instead of element node , look for text node as the node values are kept as
text node in the DOM. Otherwords once you get the Node.ELEMENT_NODE, look
for Node.TEXT_NODE and use getNodeValue for the text node.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Thursday, July 26, 2001 5:40 PM
To: [EMAIL PROTECTED]
Subject: node value
Hi
This is a simple programwhich takes file from command line.
it parses the xml file and should give node and their value.
Node are printed and not the values. can anybody help me in
getting the values.
Thanks
vijay
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.IOException;
// A Simple DOM Application
public class BasicDOM {
// Constructor
public BasicDOM (String xmlFile) {
// Create a Xerces DOM Parser
DOMParser parser = new DOMParser();
// Parse the Document
// and traverse the DOM
try {
parser.parse(xmlFile);
Document document = parser.getDocument();
traverse (document);
} catch (SAXException e) {
System.err.println (e);
} catch (IOException e) {
System.err.println (e);
}
}
// Traverse DOM Tree. Print out Element Names
private void traverse (Node node) {
int type = node.getNodeType();
if (type == Node.ELEMENT_NODE)
System.out.println (node.getNodeName());
System.out.println(node.getNodeValue());
NodeList children = node.getChildNodes();
if (children != null) {
for (int i=0; i< children.getLength(); i++)
traverse (children.item(i));
}
}
// Main Method
public static void main (String[] args) {
BasicDOM basicDOM = new BasicDOM (args[0]);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]