Java使用DOM解析xml文件

来源:互联网 发布:域名价格评估 编辑:程序博客网 时间:2024/05/16 01:31

xml文件 如下

<?xml version="1.0" encoding="UTF-8"?><bookstore><book id="156"><name>计算机网络</name><author>谢希仁</author><price>39</price><year>2013</year></book><book id="234"><name>计算机操作系统</name><author>佚名</author><price>40</price><year>2013</year><edition>第四版</edition></book><book id="367"><name>计算机组成原理</name><price>35</price><year>2013</year><edition>第三版</edition></book></bookstore>


java代码如下

package test;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.NodeList;import org.xml.sax.SAXException;public class XmlParser {public static void main(String[] args) {// 创建DocumentBuilderFactory对象DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();try {// 创建DocumentBuilder对象DocumentBuilder docBulider = dbf.newDocumentBuilder();// 创建Document对象,Document对象表示整个 HTML 或 XML 文档Document document = docBulider.parse("book.xml");// 获取book节点的集合NodeList bookList = document.getElementsByTagName("book");for (int i = 0; i < bookList.getLength(); i++) {// Element是Node的子类,Node可以通过getAttributes() 方法遍历一个节点的全部属性// 若要访问某个节点的具体的属性,只需将该节点强制类型转换成Element,使用getAttribute(String name) 即可Element book = (Element) bookList.item(i);// 获取书本的idString id = book.getAttribute("id");System.out.println("第" + (i + 1) + "本书的id是:" + id);// 获取book节点的子节点NodeList valueList = book.getChildNodes();for (int j = 0; j < valueList.getLength(); j++) {//Node节点可以是元素节点、属性节点、文本节点。//Element表示XML文档中的元素if (valueList.item(j) instanceof Element) {String name = valueList.item(j).getNodeName();// 获取属性值String value = valueList.item(j).getTextContent();System.out.println(name + "--" + value);}}System.out.println("--------------------------------");}} catch (ParserConfigurationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SAXException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}



控制台输出结果如下

第1本书的id是:156name--计算机网络author--谢希仁price--39year--2013--------------------------------第2本书的id是:234name--计算机操作系统author--佚名price--40year--2013edition--第四版--------------------------------第3本书的id是:367name--计算机组成原理price--35year--2013edition--第三版--------------------------------


其中需要注意的是Element和Node区别:

Element是Node的子类

Node表示一个节点,可以是元素节点,属性节点,也可以是文本节点

Elemen表示xml文本中的元素,即<div></div>,<book></book>这些才算一个Element元素


Node可以通过getAttributes() 方法遍历一个节点的全部属性
若要访问某个节点的具体的属性,只需将该节点强制类型转换成Element,使用getAttribute(String name) 即可


0 0