
本文共 4341 字,大约阅读时间需要 14 分钟。
XML的DOM解析 Java实现 例子二
关于节点的getNodeName()和getNodeValue()方法能得到什么值,可以查看Node类的官方文档:
The values of nodeName
, nodeValue
, and attributes
vary according to the node type as follows:
Interface | nodeName | nodeValue | attributes |
| same as | same as |
|
|
| same as |
|
|
| same as |
|
|
|
|
|
|
|
|
|
| same as |
|
|
| same as |
|
|
| entity name |
|
|
| name of entity referenced |
|
|
| notation name |
|
|
| same as | same as |
|
|
| same as |
|
See also the .
例子程序
首先是XML文档如下:
<学生名册 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation="C:\Course30\student.xsd"> <学生 学号="1"> <姓名> 张三 姓名> <性别> 男 性别> <年龄> 20 年龄> 学生> <学生 学号="2"> <姓名> 李四 姓名> <性别> 女 性别> <年龄> 19 年龄> 学生> <学生 学号="3"> <姓名> 王五 姓名> <性别> 男 性别> <年龄> 21 年龄> 学生> 学生名册>
然后是Java程序:
package com.learnjava.xml.dom;import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;public class DomTest2{ public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new File("student.xml")); // System.out.println(doc.getXmlEncoding()); // System.out.println(doc.getXmlVersion()); // System.out.println(doc.getXmlStandalone()); // 得到根节点 Element root = doc.getDocumentElement(); System.out.println("rootTagName: " + root.getTagName()); // 得到根节点的子节点,注意此处的空格也被计为子节点 NodeList list = root.getChildNodes(); System.out.println("root child Count: " + list.getLength()); for (int i = 0; i < list.getLength(); i++) { System.out.println("item " + i + ": getNodeName: " + list.item(i).getNodeName()); } System.out.println("------------NodeType and Value----------------"); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); // getNodeType返回的是short型的常量值,文档中有定义每个值对应的类型 System.out.println("getNodeType: " + n.getNodeType() + " , getNodeValue: " + n.getNodeValue()); } System.out.println("-----------getTextContent----------------"); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); System.out.println(n.getTextContent()); } System.out.println("-------------属性测试----------------"); NodeList nodeList = doc.getElementsByTagName("学生"); for (int i = 0; i < nodeList.getLength(); i++) { NamedNodeMap nnm = nodeList.item(i).getAttributes(); String attrName = nnm.item(0).getNodeName(); System.out.print("node " + i + ": " + attrName); System.out.print("="); // 属性的名字和值即为本身的名字和值 String attrValue = nnm.item(0).getNodeValue(); System.out.println(attrValue); } }}
程序输出:
rootTagName: 学生名册root child Count: 7item 0: getNodeName: #textitem 1: getNodeName: 学生item 2: getNodeName: #textitem 3: getNodeName: 学生item 4: getNodeName: #textitem 5: getNodeName: 学生item 6: getNodeName: #text------------NodeType and Value----------------getNodeType: 3 , getNodeValue: getNodeType: 1 , getNodeValue: nullgetNodeType: 3 , getNodeValue: getNodeType: 1 , getNodeValue: nullgetNodeType: 3 , getNodeValue: getNodeType: 1 , getNodeValue: nullgetNodeType: 3 , getNodeValue: -----------getTextContent---------------- 张三 男 20 李四 女 19 王五 男 21 -------------属性测试----------------node 0: 学号=1node 1: 学号=2node 2: 学号=3
参考资料
圣思园张龙老师视频教程。
Java官方文档:
发表评论
最新留言
关于作者
