欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

XML使用DOM方式解析

程序员文章站 2022-03-03 13:07:06
...
1. 元素和节点

<?xml version="1.0" encoding="UTF-8"?>
<student>
    <name id="001">张三</name>
    <sex>男</sex>
    <age>20</age>
</student>

元素一定是节点,但是节点不一定是元素。<name id="001">张三</name>
元素节点:name
属性节点:id="001"
文本节点:张三


2. DOM方式解析XML原理

基于DOM(Document Object Model,文档对象模型)解析方式,是把整个XML文档加载到内存,转化成DOM树,因此应用程序可以随机的访问DOM树的任何数据;
优点:灵活性强,速度快;
缺点:消耗资源比较多;


2.1 单节点

<?xml version="1.0" encoding="UTF-8"?>
<student>
    <name id="001">张三</name>
    <sex>男</sex>
    <age>20</age>
</student>


package com.andrew.xml;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class DOM01 {
    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse("src/student.xml");
            NodeList nodeList = document.getElementsByTagName("student");
            Element element = (Element) nodeList.item(0);
            System.out.println("姓名:" + element.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
            System.out.println("性别:" + element.getElementsByTagName("sex").item(0).getFirstChild().getNodeValue());
            System.out.println("年龄:" + element.getElementsByTagName("age").item(0).getFirstChild().getNodeValue());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
运行结果:
姓名:张三
性别:男
年龄:20


2.2 多节点

<?xml version="1.0" encoding="UTF-8"?>
<students>
    <student>
        <name id="001" xx="哈哈">张三</name>
        <sex>男</sex>
        <age>20</age>
    </student>
    <student>
        <name id="002">李四</name>
        <sex>女</sex>
        <age>21</age>
    </student>
</students>


package com.andrew.xml;
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 DOM02 {
    public static void printNodeAttr(Node node) {
        NamedNodeMap namedNodeMap = node.getAttributes();
        for (int i = 0; i < namedNodeMap.getLength(); i++) {
            Node attrNode = namedNodeMap.item(i);
            System.out.println(attrNode.getNodeName() + ":" + attrNode.getFirstChild().getNodeValue());
        }
    }
    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse("src/students.xml");
            NodeList nodeList = document.getElementsByTagName("students");
            Element element = (Element) nodeList.item(0);
            NodeList studentsNodeList = element.getElementsByTagName("student");
            for (int i = 0; i < studentsNodeList.getLength(); i++) {
                Element e = (Element) studentsNodeList.item(i);
                System.out.println("姓名:" + e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
                printNodeAttr(e.getElementsByTagName("name").item(0));
                System.out.println("性别:" + e.getElementsByTagName("sex").item(0).getFirstChild().getNodeValue());
                System.out.println("年龄:" + e.getElementsByTagName("age").item(0).getFirstChild().getNodeValue());
                System.out.println("================");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

运行结果:
姓名:张三
id:001
xx:哈哈
性别:男
年龄:20
================
姓名:李四
id:002
性别:女
年龄:21
================
相关标签: xml