package com.steven.tool;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class XmlParse {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("xml/data.xml"));
NodeList nodeList = doc.getElementsByTagName("Code");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
System.out.println(node.getFirstChild().getNodeValue());
}
NodeList nl = doc.getElementsByTagName("database");
if (nl.getLength() > 0) {
Element element = (Element) nl.item(0);
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE
&& item.getNodeName().equals("driverClass")) {
System.out.println(item.getNodeName());
System.out.println("ok:"
+ item.getFirstChild().getNodeValue());
}
}
}
}
}