深入了解DOM4J解析XML

目录

DOM4J解析XML

一 解析步骤

二 元素对象Element常用方法详解

2.1 String getName()

2.2 String getText()

2.3 String setText()

2.4 List elements() 、String attributeValue(String 属性名称)、String elementText(String 子节点名称)


DOM4J解析XML

一 解析步骤

1. 引入jar文件 dom4j.jar

2. 创建一个指向XML文件的输入流

        FileInputStream fis = new FileInputStream("xml文件的地址");

3. 创建一个XML读取工具对象

        SAXReader sr = new SAXReader();

4. 使用读取工具对象, 读取XML文档的输入流 , 并得到文档对象

        Document doc = sr.read(fis);

5. 通过文档对象, 获取XML文档中的根元素对象

        Element root = doc.getRootElement();

二 元素对象Element常用方法详解

Element指的是XML文档中的单个节点。

我们先创建一个.xml文件。

<?xml version="1.0" encoding="UTF-8"?>

<books>
    <book id="1001">
        <name>海底两万年</name>
        <info>讲述了一艘潜艇环游世界的故事</info>
    </book>
    <book id="1002">
        <name>安徒生童话</name>
        <info>为小娃娃讲故事的书</info>
    </book>
</books>

将其命名为Demo,并放到E://IO文件夹下,如图:

准备工作已做好,我们来看一下其常用的方法。

2.1 String getName()

方法作用:获取节点名称。

代码演示:

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.FileInputStream;
import java.io.IOException;

public class Demo1 {
    public static void main(String[] args) throws IOException, DocumentException {
        //创建一个指向XML文件的输入流
        FileInputStream fis = new FileInputStream("E://IO//Demo.xml");
        // 创建一个XML读取工具对象
        SAXReader sr = new SAXReader();
        //使用读取工具对象, 读取XML文档的输入流 , 并得到文档对象
        Document doc = sr.read(fis);
        //通过文档对象, 获取XML文档中的根元素对象
        Element element = doc.getRootElement();
        System.out.println(element.getName());
        fis.close();
    }

}

运行结果:

"C:\Program Files\Java\jdk-11.0.6\bin\java.exe" "-javaagent:E:\java IDEA\idea\ideaIU-2020.1.4.win\lib\idea_rt.jar=53618:E:\java IDEA\idea\ideaIU-2020.1.4.win\bin" -Dfile.encoding=UTF-8 -classpath "E:\java IDEA\java\out\production\until;E:\java IDEA\java\lib\dom4j-1.6.1.jar;E:\java IDEA\java\lib\jaxen-1.1-beta-7.jar;E:\java IDEA\java\lib\junit-4.8.jar;E:\java IDEA\java\lib\xstream-1.3.1.jar" com.demo.Demo1
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.dom4j.io.SAXContentHandler (file:/E:/java%20IDEA/java/lib/dom4j-1.6.1.jar) to method com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy.getEncoding()
WARNING: Please consider reporting this to the maintainers of org.dom4j.io.SAXContentHandler
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
books

Process finished with exit code 0

我们会看到有一堆WARNING:开头的,那个是警告,是因为我安装的JDK版本过高,但不影响代码的运行。

我们看之前的.xml,books为其根节点的名称。

2.2 String getText()

方法作用:获取节点内容

代码演示:

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.FileInputStream;
import java.io.IOException;

public class Demo1 {
    public static void main(String[] args) throws IOException, DocumentException {
        //创建一个指向XML文件的输入流
        FileInputStream fis = new FileInputStream("E://IO//Demo.xml");
        // 创建一个XML读取工具对象
        SAXReader sr = new SAXReader();
        //使用读取工具对象, 读取XML文档的输入流 , 并得到文档对象
        Document doc = sr.read(fis);
        //通过文档对象, 获取XML文档中的根元素对象
        Element root = doc.getRootElement();
        //根据子节点的名称 , 获取匹配名称的第一个子节点对象.
        Element book = root.element("book");
        //book节点里面没有内容,所以只能进name节点里面查看内容
        Element name = book.element("name");
        System.out.println(name.getText());//打印节点内容

        //关闭流
        fis.close();
    }

}

打印结果:

"C:\Program Files\Java\jdk-11.0.6\bin\java.exe" "-javaagent:E:\java IDEA\idea\ideaIU-2020.1.4.win\lib\idea_rt.jar=54527:E:\java IDEA\idea\ideaIU-2020.1.4.win\bin" -Dfile.encoding=UTF-8 -classpath "E:\java IDEA\java\out\production\until;E:\java IDEA\java\lib\dom4j-1.6.1.jar;E:\java IDEA\java\lib\jaxen-1.1-beta-7.jar;E:\java IDEA\java\lib\junit-4.8.jar;E:\java IDEA\java\lib\xstream-1.3.1.jar" com.demo.Demo1
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.dom4j.io.SAXContentHandler (file:/E:/java%20IDEA/java/lib/dom4j-1.6.1.jar) to method com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy.getEncoding()
WARNING: Please consider reporting this to the maintainers of org.dom4j.io.SAXContentHandler
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
海底两万年

Process finished with exit code 0

2.3 String setText()

方法作用:设置节点内容

代码演示:

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.FileInputStream;
import java.io.IOException;

public class Demo1 {
    public static void main(String[] args) throws IOException, DocumentException {
        //创建一个指向XML文件的输入流
        FileInputStream fis = new FileInputStream("E://IO//Demo.xml");
        // 创建一个XML读取工具对象
        SAXReader sr = new SAXReader();
        //使用读取工具对象, 读取XML文档的输入流 , 并得到文档对象
        Document doc = sr.read(fis);
        //通过文档对象, 获取XML文档中的根元素对象
        Element root = doc.getRootElement();
        //根据子节点的名称 , 获取匹配名称的第一个子节点对象.
        Element book = root.element("book");
        //book节点里面没有内容,所以只能进name节点里面查看内容
        Element name = book.element("name");
        name.setText("西游记");
        System.out.println(name.getText());


        //关闭流
        fis.close();
    }

}

打印结果:

"C:\Program Files\Java\jdk-11.0.6\bin\java.exe" "-javaagent:E:\java IDEA\idea\ideaIU-2020.1.4.win\lib\idea_rt.jar=54798:E:\java IDEA\idea\ideaIU-2020.1.4.win\bin" -Dfile.encoding=UTF-8 -classpath "E:\java IDEA\java\out\production\until;E:\java IDEA\java\lib\dom4j-1.6.1.jar;E:\java IDEA\java\lib\jaxen-1.1-beta-7.jar;E:\java IDEA\java\lib\junit-4.8.jar;E:\java IDEA\java\lib\xstream-1.3.1.jar" com.demo.Demo1
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.dom4j.io.SAXContentHandler (file:/E:/java%20IDEA/java/lib/dom4j-1.6.1.jar) to method com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy.getEncoding()
WARNING: Please consider reporting this to the maintainers of org.dom4j.io.SAXContentHandler
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
西游记

Process finished with exit code 0

2.4 List elements() 、String attributeValue(String 属性名称)、String elementText(String 子节点名称)

List elements()

方法作用:获取所有的子节点对象

String attributeValue(String 属性名称)

方法作用:获取节点的属性值

String elementText(String 子节点名称)

方法作用:获取子节点的内容

代码演示:

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class Demo1 {
    public static void main(String[] args) throws IOException, DocumentException {
        //创建一个指向XML文件的输入流
        FileInputStream fis = new FileInputStream("E://IO//Demo.xml");
        // 创建一个XML读取工具对象
        SAXReader sr = new SAXReader();
        //使用读取工具对象, 读取XML文档的输入流 , 并得到文档对象
        Document doc = sr.read(fis);
        //通过文档对象, 获取XML文档中的根元素对象
        Element root = doc.getRootElement();
        //获取子节点对象
        List<Element> ele = root.elements();
        for (int i=0;i<ele.size();i++){
            Element element = ele.get(i);
            //获取节点属性值
            System.out.println(element.attributeValue("id"));
            //获取节点name的内容
            System.out.println(element.elementText("name"));
            //获取节点info的内容
            System.out.println(element.elementText("info"));
        }

        //关闭流
        fis.close();
    }

}

打印结果:

"C:\Program Files\Java\jdk-11.0.6\bin\java.exe" "-javaagent:E:\java IDEA\idea\ideaIU-2020.1.4.win\lib\idea_rt.jar=55085:E:\java IDEA\idea\ideaIU-2020.1.4.win\bin" -Dfile.encoding=UTF-8 -classpath "E:\java IDEA\java\out\production\until;E:\java IDEA\java\lib\dom4j-1.6.1.jar;E:\java IDEA\java\lib\jaxen-1.1-beta-7.jar;E:\java IDEA\java\lib\junit-4.8.jar;E:\java IDEA\java\lib\xstream-1.3.1.jar" com.demo.Demo1
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.dom4j.io.SAXContentHandler (file:/E:/java%20IDEA/java/lib/dom4j-1.6.1.jar) to method com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy.getEncoding()
WARNING: Please consider reporting this to the maintainers of org.dom4j.io.SAXContentHandler
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
1001
海底两万年
讲述了一艘潜艇环游世界的故事
1002
安徒生童话
为小娃娃讲故事的书

Process finished with exit code 0

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值