Home > Java > javaTutorial > body text

Example analysis of how java uses dom4j to generate and parse xml documents (picture)?

黄舟
Release: 2018-05-29 17:45:25
Original
1896 people have browsed it

This article mainly introduces the method of java using dom4j to generate and parse xml documents. It combines the examples to analyze the relevant operating skills of java based on dom4j operating xml nodes to generate xml documents and parsing xml documents. Friends in need can refer to the following

The example in this article describes how Java uses dom4j to generate and parse xml documents. Share it with everyone for your reference, the details are as follows:

xml is a new data format, mainly used for data exchange. The frameworks we use all involve xml. Therefore, parsing or generating xml is also a technical difficulty for programmers. Here we use dom4j to generate a document. It should be noted that each xml document has only one root node.

package org.lxh;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class CreateXml {
  public static void main(String[] args) {
    File f=new File("d:"+File.separator+"my.xml");
    Document docu=DocumentHelper.createDocument(); //创建xml文档
    Element linkman=docu.addElement("linkman");  //创建根节点
    Element name=linkman.addElement("name"); //创建子元素
    Element age=linkman.addElement("age");
    name.setText("陈瑞银");  //设置name节点的内容
    age.setText("22");    //设置age节点的内容
    OutputFormat format=OutputFormat.createPrettyPrint(); //指定输出格式
    format.setEncoding("UTF-8");  //指定输出编码
    try {
      XMLWriter w=new XMLWriter(new FileOutputStream(f),format); //输出文件
      w.write(docu); //输出内容
      w.close();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
Copy after login

Now see if the document is generated, as shown in the figure

The document is generated. This document is relatively simple. The same goes for generating complex documents. Let's parse this xml.

The code is as follows

package org.lxh;
import java.io.File;
import java.util.Iterator;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
public class ReadXml {
  public static void main(String[] args) {
    File f=new File("d:"+File.separator+"my.xml");
    SAXReader read=new SAXReader();  //建立SAX解析读取
    Document document=null;
    try {
      document=read.read(f);  //读取文档
      Element root=document.getRootElement();  //取得根元素
      //下面给注释的部分用于解析复杂的xml(3层或以上)
      /*Iterator it=root.elementIterator();  //取得全部子节点
      while(it.hasNext())
      {
        /*Element e=(Element)it.next();
        System.out.println(e.elementText("name")); //取得文本元素
        System.out.println(e.elementText("age"));
      }*/
      System.out.println(root.elementText("age"));
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
Copy after login

The following is a screenshot of the running effect

The above is the detailed content of Example analysis of how java uses dom4j to generate and parse xml documents (picture)?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!