Parsing Problem
843834Aug 14 2001 — edited Aug 16 2001My XML file looks like this .....
<properties>
<property>
<name>Sybase Server</name>
<value>OMSDEVL</value>
</property>
<property>
<name>Database</name>
<value>librarydb</value>
</property>
<property>
<name>table-name</name>
<value>BOOKS_MEDIA</value>
</property>
<property>
<name>csv-file</name>
<value>library.txt</value>
</property>
<property>
<name>format-file</name>
<value>library.fmt</value>
</property>
</properties>
and the program looks like that...
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.SAXException;
public class Test {
public void domParse(String url) {
DocumentBuilder parser;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
parser = factory.newDocumentBuilder();
Document doc = parser.parse(url);
Node myNode = doc.getElementsByTagName("properties").item(0);
if (myNode != null) {
NodeList propChildren = myNode.getChildNodes();
int n = propChildren.getLength();
for (int i=0;i<n;++i) {
Node node = propChildren.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
NodeList nodeChildren = node.getChildNodes();
int m = nodeChildren.getLength();
System.out.println("<" + node.getNodeName() + ">");
for (int j=0;j<m;++j)
if (nodeChildren.item(j).getNodeType() == Node.ELEMENT_NODE) {
Node thisnode = nodeChildren.item(j);
System.out.println("<" + thisnode.getNodeValue() + ">");
}
}
}
}
else
System.out.println("element <properties> not found");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Test atest = new Test();
atest.domParse("C:/temp/properties.xml");
}
}
The output is coming like that
C:\Testing>java Test
<property>
<null>
<null>
<property>
<null>
<null>
<property>
<null>
<null>
<property>
<null>
<null>
<property>
<null>
<null>
Can anybody help?
ab