Home  >  Article  >  Java  >  Detailed explanation of the implementation method of dom4j creating and parsing xml documents

Detailed explanation of the implementation method of dom4j creating and parsing xml documents

怪我咯
怪我咯Original
2017-07-02 10:32:581333browse

The following editor will bring you an implementation method of creating and parsing xml documents with dom4j. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look

DOM4J analysis

##Features:

1. An intelligent fork of JDOM that incorporates many features beyond basic XML document representation.

2. It uses

interface and abstract basic class methods.

3. It has the characteristics of excellent performance, good flexibility, powerful functions and extreme ease of use.

4. It is an open source file

jar package: dom4j-1.6.1.jar

# #Create book.xml:

package com.example.xml.dom4j;

import java.io.FileWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
/**
 * dom4j创建xml文档示例
 *
 */
public class Dom4JTest4 {
  public static void main(String[] args) throws Exception {
    // 第二种方式:创建文档并设置文档的根元素节点
    Element root2 = DocumentHelper.createElement("bookstore");
    Document document2 = DocumentHelper.createDocument(root2);

    // 添加一级子节点:add之后就返回这个元素
    Element book1 = root2.addElement("book");
    book1.addAttribute("id", "1");
    book1.addAttribute("name", "第一本书");
    // 添加二级子节点
    book1.addElement("name").setText("遇见未知的自己");
    book1.addElement("author").setText("张德芬");
    book1.addElement("year").setText("2014");
    book1.addElement("price").setText("109");
    // 添加一级子节点
    Element book2 = root2.addElement("book");
    book2.addAttribute("id", "2");
    book2.addAttribute("name", "第二本书");
    // 添加二级子节点
    book2.addElement("name").setText("双城记");
    book2.addElement("author").setText("狄更斯");
    book2.addElement("year").setText("2007");
    book2.addElement("price").setText("29");
    
    // 设置缩进为4个空格,并且另起一行为true
    OutputFormat format = new OutputFormat("  ", true);
 
    // 另一种输出方式,记得要调用flush()方法,否则输出的文件中显示空白
    XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("book.xml"),format);
    xmlWriter3.write(document2);
    xmlWriter3.flush();
    // close()方法也可以

  }
}
Run result:

##Parse book.xml:

package com.example.xml.dom4j;

import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
 * dom4j解析xml文档示例
 *
 */
public class Dom4JTest3 {
  
  public static void main(String[] args) {
    // 解析books.xml文件
    // 创建SAXReader的对象reader
    SAXReader reader = new SAXReader();
    try {
      // 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。
      Document document = reader.read(new File("book.xml"));
      // 通过document对象获取根节点bookstore
      Element bookStore = document.getRootElement();
      System.out.println("根节点名:"+bookStore.getName());
      // 通过element对象的elementIterator方法获取迭代器
      Iterator it = bookStore.elementIterator();
      // 遍历迭代器,获取根节点中的信息(书籍)
      while (it.hasNext()) {
        System.out.println("=====开始遍历子节点=====");
        Element book = (Element) it.next();
        System.out.println("子节点名:"+book.getName());
        // 获取book的属性名以及 属性值
        List<Attribute> bookAttrs = book.attributes();
        for (Attribute attr : bookAttrs) {
          System.out.println("属性名:" + attr.getName() + "--属性值:"
              + attr.getValue());
        }
        Iterator itt = book.elementIterator();
        while (itt.hasNext()) {
          Element bookChild = (Element) itt.next();
          System.out.println("节点名:" + bookChild.getName() + "--节点值:" + bookChild.getStringValue());
        }
        System.out.println("=====结束遍历该节点=====");
      }
    } catch (DocumentException e) {
      e.printStackTrace();
    }
  }

}
Run result:

The above is the detailed content of Detailed explanation of the implementation method of dom4j creating and parsing xml documents. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn