DOM4J解析xml(图文教程)

本文介绍了如何在Spring Boot项目中集成dom4j和相关依赖,展示了如何使用DOM解析XML文件,并通过DOMTest类进行操作。重点在于Spring Boot启动器和处理XML数据的实践应用。
摘要由CSDN通过智能技术生成

1、pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2-saaj -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-saaj</artifactId>
            <version>1.7.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.1.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.axis/axis -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.axis/axis-jaxrpc -->
        <dependency>
            <groupId>org.apache.axis</groupId>
            <artifactId>axis-jaxrpc</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-discovery/commons-discovery -->
        <dependency>
            <groupId>commons-discovery</groupId>
            <artifactId>commons-discovery</artifactId>
            <version>0.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、test:

package com.example.test;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class DOMTest {
    public static void main(String[] args) throws Exception {
        // 创建一个DocumentBuilderFactory的对象
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        // 创建DocumentBuilder对象
        DocumentBuilder db = dbf.newDocumentBuilder();
        // 通过DocumentBuilder对象的parser方法加载books.xml文件到当前项目下
        InputStream is = new FileInputStream(new File("D:\\code\\demo1\\src\\main\\java\\com\\example\\books.xml"));
        Document document = db.parse(is);
        // 获取所有book节点的集合
        NodeList bookList = document.getElementsByTagName("book");
        // 通过bookList的getLength()方法可以获取bookList的长度
        System.out.println("一共有" + bookList.getLength() + "本书");
        // 遍历每一个book节点
        for (int i = 0; i < bookList.getLength(); i++) {
            System.out.println("=========下面开始遍历第" + (i + 1) + "本书的内容=========");
            // 通过 item(i)方法 获取一个book节点,booklist的索引值从0开始
            Node book = bookList.item(i);
            // 获取book节点的所有属性集合
            NamedNodeMap attrs = book.getAttributes();
            System.out.println("第 " + (i + 1) + "本书共有" + attrs.getLength() + "个属性");
            // 遍历book的属性
            for (int j = 0; j < attrs.getLength(); j++) {
                // 通过item(index)方法获取book节点的某一个属性
                Node attr = attrs.item(j);
                // 获取属性名
                System.out.print("属性名:" + attr.getNodeName());
                // 获取属性值
                System.out.println("--属性值" + attr.getNodeValue());
            }
            // 解析book节点的子节点
            NodeList childNodes = book.getChildNodes();
            // 遍历childNodes获取每个节点的节点名和节点值
            System.out.println("第" + (i + 1) + "本书共有" + childNodes.getLength() + "个子节点");
            for (int k = 0; k < childNodes.getLength(); k++) {
                // 区分出text类型的node以及element类型的node
                if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
                    // 获取了element类型节点的节点名
                    System.out.print("第" + (k + 1) + "个节点的节点名:" + childNodes.item(k).getNodeName());
                    // 获取了element类型节点的节点值
                    System.out.println("--节点值是:" + childNodes.item(k).getFirstChild().getNodeValue());
                    // System.out.println("--节点值是:" + childNodes.item(k).getTextContent());
                }
            }
            System.out.println("=========结束遍历第" + (i + 1) + "本书的内容=========");
        }

    }}

3、xml:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book id="1">
        <name>冰与火之歌</name>
        <author>乔治马丁</author>
        <year>2014</year>
        <price>89</price>
    </book>
    <book id="2">
        <name>安徒生童话</name>
        <year>2004</year>
        <price>77</price>
        <language>English</language>
    </book>    
</bookstore>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

91科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值