dom4j解析xml

准备阶段
dom4j作为解析xml的一把利器,学习使用dom4j是必须的。
要想使用dom4j解析xml,就得先准备好dom4j文件。
这里给出一个github的下载地址:https://dom4j.github.io/,在下载时要根据自己的jdk版本“量力而行”。如果地址失效了,可以百度、谷歌搜索。
下载解压后将dom4j对应的jar包加入到你的工程即可使用。
1、进入解压的文件中,复制dom4j对应的jar包。
这里写图片描述

2、在你的项目中创建一个lib目录,将jar包放入lib中。
这里写图片描述

3、将这个jar加到项目中。
这里写图片描述
添加后显示:
这里写图片描述
表示上传成功。

使用dom4j
使用到的xml文档

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

<书架> 
  < id="b1"> 
    <书名>Book1</书名>  
    <作者>auther1</作者>  
    <售价>50元</售价> 
  </>  
  < id="b2"> 
    <书名>Book2</书名>  
    <作者>auther2</作者>  
    <售价>价格2</售价> 
  </>  
</书架>

1,读取xml文档

public class Deom1 {

    public static void main(String[] args) throws Exception {
        // 创建SAXReader对象
        SAXReader read = new SAXReader();
        // 读取文件
        Document doc = read.read("src/testBook.xml");

        // 获取根节点
        Element root = doc.getRootElement();

        // 遍历节点
        // 得到根节点下面的所有节点
        List lists = root.elements();
        for(int i = 0; i < lists.size(); i++){
            // 得到一个节点
            Element list = (Element) lists.get(i);

            // 遍历节点的属性
            System.out.println("------遍历第"+(i+1)+"个节点属性------");
            List attrs = list.attributes();
            for(int j = 0; j < attrs.size(); j++){
                Attribute attr = (Attribute) attrs.get(j);
                System.out.println(list.getName()+"的属性:"+attr.getName()+"-"+attr.getValue());
            }

            // 遍历子节点
            System.out.println("------遍历子节点------");
            List eles = list.elements();
            for(int x = 0; x < eles.size(); x++){
                Element ele = (Element) eles.get(x);
                System.out.println(ele.getName()+"的内容是: "+ele.getStringValue());
            }
            System.out.println("-----结束遍历第"+(i+1)+"个节点-----");
    }

}

运行结果:
这里写图片描述

2、添加节点:在第一本书的作者前面添加 出版社 节点 以及添加一本新书

public class Deom1 {

    public static void main(String[] args) throws Exception {
        // 创建SAXReader对象,读取文件
        SAXReader read = new SAXReader();
        Document doc = read.read("src/testBook.xml");

        // 得到根节点
        Element root = doc.getRootElement();

        // 得到第一本书的节点
        Element ele = (Element) root.elements().get(0);
        // 创建要添加的节点
        Element cbs = DocumentHelper.createElement("出版社");
        cbs.setText("出版社1");
        // 将节点添加到指定的位置
        List lists = ele.elements();
        lists.add(1, cbs);

        // 添加一本书
        // 在书架节点下创建书
        Element book = root.addElement("书");
        // 设置书的属性
        book.addAttribute("id", "b3");
        // 在书的节点下添加 书名 节点
        Element name = book.addElement("书名");
        // 设置内容
        name.setText("Book3");

        // 在写入xml文件时,使用“宽松”的排版
        OutputFormat of = OutputFormat.createPrettyPrint();
        // 使用“紧凑”的排版
//      OutputFormat of = OutputFormat.createCompactFormat();
        // 设置编码格式
        of.setEncoding("UTF-8");
        // 将读到内存中的文档写回原来的位置
        XMLWriter write = new XMLWriter(new FileOutputStream("src/testBook.xml"), of);
        write.write(doc);
        // 关闭输出流
        write.close();
    }
}

使用“宽松”排版的结果:

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

<书架>
  < id="b1">
    <书名>Book1</书名>
    <出版社>出版社1</出版社>
    <作者>auther1</作者>
    <售价>50元</售价>
  </>
  < id="b2">
    <书名>Book2</书名>
    <作者>auther2</作者>
    <售价>价格2</售价>
  </>
  < id="b3">
    <书名>Book3</书名>
  </>
</书架>

使用“紧凑”排版的结果:

<?xml version="1.0" encoding="UTF-8"?>
<书架>< id="b1"><书名>Book1</书名><出版社>出版社1</出版社><出版社>出版社1</出版社><作者>auther1</作者><售价>50元</售价></>< id="b2"><书名>Book2</书名><作者>auther2</作者><售价>价格2</售价></>< id="b3"><书名>Book3</书名></>< id="b3"><书名>Book3</书名></></书架>

添加节点的步骤:需要在哪个节点身上添加节点,就是用这个节点创建目标节点,然后设置。
jaxp添加节点的步骤:先创建设置好要添加的节点,然后将节点挂载到想要的节点上。

3、删除节点:将第一本书的 作者 节点 删除

public class Deom1 {

    public static void main(String[] args) throws Exception {
        SAXReader read = new SAXReader();
        Document doc = read.read("src/testBook.xml");
        Element root = doc.getRootElement();

        // 得到目标节点
        Element ele = root.element("书").element("作者");
        // 删除目标节点
        ele.getParent().remove(ele);

        OutputFormat of = OutputFormat.createPrettyPrint();
        of.setEncoding("UTF-8");
        XMLWriter write = new XMLWriter(new FileOutputStream("src/testBook.xml"), of);
        write.write(doc);
        write.close();
    }

}

运行结果:

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

<书架> 
  < id="b1"> 
    <书名>Book1</书名>  
    <售价>50元</售价> 
  </>  
  < id="b2"> 
    <书名>Book2</书名>  
    <作者>auther2</作者>  
    <售价>价格2</售价> 
  </>  
</书架>

删除节点的步骤:得到目标节点,然后使用getParent().remove()删除目标节点。

4、更新节点:将第二本书的 作者更改

public class Deom1 {

    public static void main(String[] args) throws Exception {
        SAXReader read = new SAXReader();
        Document doc = read.read("src/testBook.xml");
        Element root = doc.getRootElement();

        // 得到目标节点
        Element ele = (Element) root.elements("书").get(1);
        Element e = ele.element("作者");
        // 修改值
        e.setText("auther222");

        OutputFormat of = OutputFormat.createPrettyPrint();
        of.setEncoding("UTF-8");
        XMLWriter wr = new XMLWriter(new FileOutputStream("src/testBook.xml"), of);
        wr.write(doc);
        wr.close();
    }

}

运行结果:

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

<书架> 
  < id="b1"> 
    <书名>Book1</书名>  
    <作者>auther1</作者>  
    <售价>50元</售价> 
  </>  
  < id="b2"> 
    <书名>Book2</书名>  
    <作者>auther222</作者>  
    <售价>价格2</售价> 
  </>  
</书架>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值