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

通过 DOM4J 操作 XML

程序员文章站 2022-03-03 16:14:06
...

1. 创建XML文档

方法一:

        Document  document = DocumentHelper.createDocument();
        Element  root = DocumentHelper.createElement("stus");
        document.setRootElement(root);

方法二:

        Element root = DocumentHelper.createElement("stus");
        Document document = DocumentHelper.createDocument(root);


2. 添加属性

    stu1.addAttribute("id","01");

3. 添加子元素及设置元素内容


     在 Branch 接口中定义了 AddElement() 方法
     public Element addElement(String name)


     Element name = stu1.addElement("name");
     name.setText("zhangsan");

4. 输出文档

在 org.dom4j.io 包中,DOMWriter 类可以将 dom4j 树输出为 W3C DOM 的 Document 对象,SAXWriter  类可以将  dom4j 树作为 SAX 事件流输出,而  XMLWriter  类则提供了将  dom4j  树的各个部分以  XML 流的方式输出能力。

       //XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("res/stus2.xml"));

        XMLWriter xmlWriter = new XMLWriter(new FileWriter("res/stus2.xml"));
        xmlWriter.write(document);
        xmlWriter.close();
 

5. 控制文档输出格式

        // org.dom4j.io.OutputFormat;
        // 缩进和换行
        OutputFormat format = new OutputFormat("    ",true);
        // 使用美化格式
        OutputFormat format1 = OutputFormat.createPrettyPrint();
        format1.setEncoding("utf-8");
        format1.setIndent("    ");

package com.dom4j;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;

public class demo01 {
    public static void main(String[] args) throws Exception {

        /*Document document = DocumentHelper.createDocument();
        Element root = DocumentHelper.createElement("stus");
        document.setRootElement(root);*/

        Element root = DocumentHelper.createElement("stus");
        Document document = DocumentHelper.createDocument(root);

        Element stu1 = root.addElement("stu");
        stu1.addAttribute("id","01");
        Element name = stu1.addElement("name");
        name.setText("张三");
        Element age = stu1.addElement("age");
        Element sex = stu1.addElement("sex");

        name.setText("zhangsan");
        age.setText("18");
        sex.setText("男");

        // org.dom4j.io.OutputFormat;
        // 缩进和换行
        OutputFormat format = new OutputFormat("    ",true);
        // 使用美化格式
        OutputFormat format1 = OutputFormat.createPrettyPrint();
        format1.setEncoding("utf-8");
        format1.setIndent("    ");

        //XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("res/stus2.xml"));
        XMLWriter xmlWriter = new XMLWriter(new FileWriter("res/stus2.xml"),format1);
        xmlWriter.write(document);
        xmlWriter.close();
        System.out.println("OVER");

    }
}

执行结果:

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

<stus>
    <stu id="01">
        <name>zhangsan</name>
        <age>18</age>
        <sex>男</sex>
    </stu>
</stus>

 

6. 构建dom4j树


dom4j 在 org.dom4j.io 包中,提供了两个类: DOMReader 和 SAXReader

DOMReader 从一个现有的 W3C DOM 树构建 dom4j 树,而 SAXReader 则使用 SAX 解析器,从不同的输入源来构造 dom4j树。

 //SAXReader 类来构造dom4j 文档对象
        SAXReader saxReader = new SAXReader();
        org.dom4j.Document document = saxReader.read(new File("res/stus.xml"));

        //DOMReader类构造dom4j文档对象
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        org.w3c.dom.Document document1= documentBuilder.parse("res/stus.xml");

        DOMReader domReader = new DOMReader();
        org.dom4j.Document document2 = domReader.read(document1);


7. 访问根元素

document.getRootElement();
 

8、访问节点


Java.util.List childrenLis=elt.elements();
Java.util.List childrenList=elt.elements(“hi.xml”);
Element eltChild=elt.element(“hi.xml”);
要迭代某个元素的所有子元素
    for(java.util.Iterator it=root.elementIterator();it.hasNext();)
     {
       Element element=(Element)it.next();
      ……
    }


9. 删除某个元素

 可以用Branch接口中的remove方法

Element eltStru=root.element(“student”);

 root.remove(eltStu);

10. 访问属性

    java.util.List attrList=elt.attributes();
   Attribute attr=elt.attribute(“sn”);
   String attrValue=elt.attributeValue();
   elt.remove(elt.attribute(“sn”));
 

package com.dom4j;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.DOMReader;
import org.dom4j.io.SAXReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.Iterator;
import java.util.List;

public class Demo02 {
    public static void main(String[] args) throws Exception{

        //SAXReader 类来构造dom4j 文档对象
        SAXReader saxReader = new SAXReader();
        org.dom4j.Document document = saxReader.read(new File("res/stus2.xml"));

        //DOMReader类构造dom4j文档对象
      /*  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        org.w3c.dom.Document document1= documentBuilder.parse("res/stus.xml");

        DOMReader domReader = new DOMReader();
        org.dom4j.Document document2 = domReader.read(document1);*/

        Element root = document.getRootElement();

        //root.element("stu");
        List<Element> elements = root.elements();

        Iterator iterator = root.elementIterator();

        while (iterator.hasNext()){
            Element stu = (Element)iterator.next();

            System.out.println("学号:"+stu.attributeValue("id"));
            System.out.println("姓名:"+stu.elementText("name"));
            System.out.println("年龄:"+stu.elementText("age"));
            System.out.println("性别:"+stu.elementText("sex"));

            System.out.println("+++++++++++++++++++");
        }

    }
}

 

 

相关标签: XML DOM DOM4J