java解析xml

解析的xml

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book >
        <name>Java</name>
        <author>Bruce Eckel</author>
        <year>2014</year>
        <price>102</price>
    </book>
    <book >
        <name>JavaScript</name>
        <year>2012</year>
        <price>86</price>
        <language>Chinese</language>
    </book> 
</bookstore>

1.dom解析xml

package www.it.com.parse;

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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * @author wangjie:
 * @version 创建时间:2019年8月30日 上午11:58:03
 * @Description 类描述:
 */
public class DomParseXml {

        public static void main(String[] args) {
                // 创建解析工厂
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                try {
                        // 创建文档解析器
                        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
                        // 文档解析器解析xml获得文档对象
                        Document document = documentBuilder.parse("E://book.xml");
                        // 通过节点的名字获取节点的集合对象
                        NodeList nodeList = document.getElementsByTagName("book");
                        System.out.println("==========book节点数" + nodeList.getLength());
                        // 循环遍历节点集合
                        for (int i = 0; i < nodeList.getLength(); i++) {
                                Node item = nodeList.item(i);
                                System.out.println("上一级几点" + item.getNodeName());
                                // 获得节点的属性
                                NodeList childNodes = item.getChildNodes();
                                System.out.println("=======" + childNodes.getLength());
                                for (int j = 1; j < childNodes.getLength(); j++) {
                                        Node childNode = childNodes.item(j);
                                        // 12.区分出text类型和element类型的node
                                        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                                                System.out.println("book节点下面的子节点" + "节点的标签" + childNode.getNodeName() + "节点的内容"
                                                                + childNode.getTextContent());
                                        }
                                }
                        }
                } catch (ParserConfigurationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (SAXException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }

        }
}