xml解析-dom4j范例

前端之家收集整理的这篇文章主要介绍了xml解析-dom4j范例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

dom4j是一个第三方xml解析库,所以要在工程中加引用库。
下载了dom4j项目之后,解压缩,将其目录下面jar包加入class path下面。
 (Properties->Java Build Path -> Add External JARs…)加入工程就可以使用了。

这里分两个例程,一个创建xml文档,一个读取xml文档并解析

创建xml文档并添加节点数据

package dom4j;

import java.io.FileOutputStream;
import java.io.IOException;

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

public class Dom4jDemo {

    public Dom4jDemo() {
        // TODO Auto-generated constructor stub
    }

    /** * 创建xml文档,并添加元素。 * * @param args * @throws IOException */
    public static void main(String[] args) throws IOException {
        // 第一种方式:创建文档节点
        Document document = DocumentHelper.createDocument();

        // 创建根节点,并添加到文档节点中
        Element element = DocumentHelper.createElement("student");
        document.setRootElement(element);
        XMLWriter writer = new XMLWriter();
        writer.write(document);//可以直接把document节点下所有数据打印出来

        // 第二种方式,先创建文档根节点,然后创建文档节点。并将根节点引用作为参数
        Element element2 = DocumentHelper.createElement("students");// 头节点students
        Document document1 = DocumentHelper.createDocument(element2);
        Element student1 = element2.addElement("student");// 头节点创建子节点
        student1.addAttribute("name","vincent");// 子节点属性

        Element birthday = student1.addElement("birthday");// 子节点的子节点
        birthday.setText("1993-01-21");
        Element school = student1.addElement("school");
        school.setText("GDUT");
        writer.write(document1);// 输出到终端
        // 输出文件,格式化输出,第一个参数表示子节点和父节点要缩进多少个空格
        //第二个参数表示节点间是否要另起一行。每个新的节点都是独自一行
        OutputFormat format = new OutputFormat(" ",true);
        writer = new XMLWriter(new FileOutputStream("src/student.xml"),format);
        Element student2 = element2.addElement("student");
        student2.addAttribute("name","ok");
        Element student2_birthday = student2.addElement("birthday");
        student2_birthday.setText("1232-3-12");
        Element student2_school = student2.addElement("school");
        student2_school.setText("GDUT");

        writer.write(document1);
        writer.flush();
        writer.close();

    }
}

输出的结果是

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

<students>
    <student name="vincent">
        <birthday>1993-01-21</birthday>
        <school>GDUT</school>
    </student>
    <student name="ok">
        <birthday>1232-3-12</birthday>
        <school>GDUT</school>
    </student>
</students>

解析生成的这个xml文档

package dom4j;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ParseDom4j {

    public ParseDom4j() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) throws DocumentException {
        //saxreader是通过sax事件获取(构造)dom4j树
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File("src/student.xml"));
        System.out.println("Docment Node Name " + document.getName());
        Element root = document.getRootElement();
        System.out.println("Root Name" + root.getName());
        List<Element> elements = root.elements();// 获取根元素下的所有子节点
        System.out.println("根元素下的所有子节点数" + elements.size());
        List<Element> students = root.elements("student");// 获取根元素下的所有student子节点
        System.out.println("根元素下的所有student节点数" + students.size());
        for (Iterator iterator = students.iterator(); iterator.hasNext();) {
            Element element = (Element) iterator.next();
            System.out.println(element.attributeValue("name"));
            List<Element> childNode = element.elements();
            for (Iterator iterator2 = childNode.iterator(); iterator2.hasNext();) {
                Element childElement = (Element) iterator2.next();
                System.out.println(childElement.getName() + "\t\t" + childElement.getText());

            }
        }

    }

}

输出

Docment Node Name file:///home/cindy/eclipseworkspace/XMLParseSax/src/student.xml Root Names tudents 根元素下的所有子节点数2 根元素下的所有student节点数2 vincent birthday 1993-01-21 school GDUT ok birthday 1232-3-12 school GDUT

原文链接:https://www.f2er.com/xml/295060.html

猜你在找的XML相关文章