您的位置:首页 > 其它

dom解析xml的简单实例

2011-07-14 20:02 330 查看
public class ParseXML {
public static void main(String[] args) throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse("c:/test.xml");
NodeList nl = d.getElementsByTagName("to");
Node n = nl.item(0);
System.out.println(n.getNodeName());
System.out.println(n.getNodeValue());
System.out.println(n.getNodeType());

Node node = n.getFirstChild();
System.out.println(node.getNodeName());
System.out.println(node.getNodeValue());
System.out.println(node.getNodeType());

NodeList nl2 = d.getElementsByTagName("from");
Node n2 = nl2.item(0);
System.out.println(n2.getNodeName());
System.out.println(n2.getNodeValue());
System.out.println(n2.getNodeType());

Node node2 = n2.getFirstChild();
System.out.println(node2.getNodeName());
System.out.println(node2.getNodeValue());
System.out.println(node2.getNodeType());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: