You need set parser.setIncludeIgnorableWhitespace(false);
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Thursday, July 26, 2001 5:31 PM
To: [EMAIL PROTECTED]
Subject: RE: node value
Thanks Binu
The code is now running. it is giving values again null
xml is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE WEATHER SYSTEM "Weather.dtd">
<WEATHER>
<CITY NAME="New York">
<TEMP>64
</CITY>
</WEATHER>
dtd is
<!ELEMENT WEATHER (CITY)+>
<!ELEMENT CITY (TEMP)>
<!ATTLIST CITY NAME CDATA #REQUIRED>
<!ELEMENT TEMP (#PCDATA)>
code is
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
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() +
getElementTextValue(node));
NodeList children = node.getChildNodes();
if (children != null) {
for (int i=0; i< children.getLength(); i++)
traverse (children.item(i));
}
}
private String getElementTextValue(Node e) {
Node child = e.getFirstChild();
if (child.getNodeType() == Node.TEXT_NODE){
child.getNodeValue().toString();
}
return null;
}
// 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]