Home  >  Article  >  Backend Development  >  Detailed introduction to the basics of xml parsing java

Detailed introduction to the basics of xml parsing java

黄舟
黄舟Original
2017-03-18 16:55:041305browse

dom4j is a Java XML API, similar to jdom, used to read and write XML files. dom4j is a very, very excellent Java XML API with excellent performance, powerful functions and extreme ease of use. It is also an open source software and can be found on SourceForge.
For mainstream Java XML In terms of API performance, functionality and ease of use, dom4j is excellent in every aspect. Nowadays you can see that more and more Java software is using dom4j to read and write XML, such as Hibernate, including Sun's own JAXM, which also uses Dom4j.
Use Dom4j to develop, you need to download the corresponding jar file of dom4j

  1. Download the corresponding jar file of dom4j
    2.dom4j is an open source project on sourceforge.net, so you can Go to http://sourceforge.net/projects/dom4j to download the latest version.
    The effect after decompressing the downloaded zip file is as follows:

Open dom4j- 1.6.1 decompression file
Here you can see the folder with docs help, and the dom4j-1.6.1.jar file that needs to use dom4j to parse the xml file. We only need to put the dom4j-1.6.1.jar file After building it into the project we develop, we can use dom4j to develop it.
Below I will take the construction method of creating a Java project with Myeclipse as an example.
First create a demo project, create a lib file in the demo project, and put Copy the dom4j-1.6.1.jar file to lib, then right-click the dom4j-1.6.1jar file
Click Add to Build Path to build it into the project.
Note: If you are developing a web project , we only need to copy it to web-inf/lib, and it will be automatically built into the web project.
During the project development process, you can refer to the docs folder (help document) to find index.html Open it and click Quick start to learn dom4j to parse xml through the help document.
Below I will translate and explain the important methods in the api that I think are as follows:

1. In DOM4j, get DocumentObject There are three ways:

1. Read the XML file and get the document object                    

 SAXReader reader = new SAXReader();                
       Document   document = reader.read(new File("csdn.xml"));

2. Parse the text in XML form and get the document object.            

  String text = "<csdn></csdn>";              
                  Document document = DocumentHelper.parseText(text);

3. Actively create document objects.
           

 Document document = DocumentHelper.createDocument();             //创建根节点  
                 Element root = document.addElement("csdn");

2. Methods of operating node objects

1.获取文档的根节点.  
      Element root = document.getRootElement();  
    2.取得某个节点的子节点.  
      Element element=node.element(“四大名著");  
    3.取得节点的文字  
        String text=node.getText();  
    4.取得某节点下所有名为“csdn”的子节点,并进行遍历.  
       List nodes = rootElm.elements("csdn");   
         for (Iterator it = nodes.iterator(); it.hasNext();) {     
      Element elm = (Element) it.next();    
    // do something   }  
     5.对某节点下的所有子节点进行遍历.      
      for(Iterator it=root.elementIterator();it.hasNext();){        
        Element element = (Element) it.next();        
       // do something    }  
    6.在某节点下添加子节点  
      Element elm = newElm.addElement("朝代");  
    7.设置节点文字.  elm.setText("明朝");  
    8.删除某节点.//childElement是待删除的节点,parentElement是其父节点  parentElement.remove(childElment);  
    9.添加一个CDATA节点.Element contentElm = infoElm.addElement("content");contentElm.addCDATA(“cdata区域”);

3. Properties of node objectsMethod operation

1.取得某节点下的某属性    Element root=document.getRootElement();        //属性名name  
         Attribute attribute=root.attribute("id");  
    2.取得属性的文字  
    String text=attribute.getText();  
    3.删除某属性 Attribute attribute=root.attribute("size"); root.remove(attribute);  
    4.遍历某节点的所有属性     
      Element root=document.getRootElement();        
       for(Iterator it=root.attributeIterator();it.hasNext();){          
           Attribute attribute = (Attribute) it.next();           
           String text=attribute.getText();          
           System.out.println(text);    
  }  
    5.设置某节点的属性和文字.   newMemberElm.addAttribute("name", "sitinspring");  
    6.设置属性的文字   Attribute attribute=root.attribute("name");   attribute.setText("csdn");

4. Write the document into the XML file

1.文档中全为英文,不设置编码,直接写入的形式.    
       XMLWriter writer = new XMLWriter(new  FileWriter("ot.xml"));   
       writer.write(document);    
       writer.close();  
    2.文档中含有中文,设置编码格式写入的形式.  
       OutputFormat format = OutputFormat.createPrettyPrint();// 创建文件输出的时候,自动缩进的格式                    
       format.setEncoding("UTF-8");//设置编码  
       XMLWriter writer = new XMLWriter(newFileWriter("output.xml"),format);  
       writer.write(document);  
       writer.close();

5. Conversion of String and XML

1.将字符串转化为XML  
      String text = "<csdn> <java>Java班</java></csdn>";  
      Document document = DocumentHelper.parseText(text);  
    2.将文档或节点的XML转化为字符串.  
       SAXReader reader = new SAXReader();  
       Document   document = reader.read(new File("csdn.xml"));              
       Element root=document.getRootElement();      
       String docXmlText=document.asXML();  
       String rootXmlText=root.asXML();  
       Element memberElm=root.element("csdn");  
       String memberXmlText=memberElm.asXML();

6. Case ( Parse the sida.xml file and perform curd operations on it)

1.sida.xml describes the operations of the four famous works. The file content is as follows

<?xml version="1.0" encoding="UTF-8"?>  
<四大名著>  
    <西游记 id="x001">  
        <作者>吴承恩1</作者>  
        <作者>吴承恩2</作者>  
        <朝代>明朝</朝代>  
    </西游记>  
    <红楼梦 id="x002">  
        <作者>曹雪芹</作者>  
    </红楼梦>  
</四大名著>

2. Analysis class test operation

package dom4j;  
  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.FileWriter;  
import java.io.OutputStreamWriter;  
import java.nio.charset.Charset;  
import java.nio.charset.CharsetEncoder;  
import java.util.Iterator;  
import java.util.List;  
  
import org.dom4j.Attribute;  
import org.dom4j.Document;  
import org.dom4j.Element;  
import org.dom4j.io.OutputFormat;  
import org.dom4j.io.SAXReader;  
import org.dom4j.io.XMLWriter;  
import org.junit.Test;  
  
public class Demo01 {  
  
    @Test  
    public void test() throws Exception {  
  
        // 创建saxReader对象  
        SAXReader reader = new SAXReader();  
        // 通过read方法读取一个文件 转换成Document对象  
        Document document = reader.read(new File("src/dom4j/sida.xml"));  
        //获取根节点元素对象  
        Element node = document.getRootElement();  
        //遍历所有的元素节点          listNodes(node);  
  
        // 获取四大名著元素节点中,子节点名称为红楼梦元素节点。  
        Element element = node.element("红楼梦");  
        //获取element的id属性节点对象  
        Attribute attr = element.attribute("id");  
        //删除属性          element.remove(attr);  
        //添加新的属性  
        element.addAttribute("name", "作者");  
        // 在红楼梦元素节点中添加朝代元素的节点  
        Element newElement = element.addElement("朝代");  
        newElement.setText("清朝");  
        //获取element中的作者元素节点对象  
        Element author = element.element("作者");  
        //删除元素节点  
        boolean flag = element.remove(author);  
        //返回true代码删除成功,否则失败          System.out.println(flag);  
        //添加CDATA区域  
        element.addCDATA("红楼梦,是一部爱情小说.");  
        // 写入到一个新的文件中          writer(document);  
  
    }  
  
    /** 
     * 把document对象写入新的文件 
     *  
     * @param document 
     * @throws Exception 
     */  
    public void writer(Document document) throws Exception {  
        // 紧凑的格式  
        // OutputFormat format = OutputFormat.createCompactFormat();  
        // 排版缩进的格式  
        OutputFormat format = OutputFormat.createPrettyPrint();  
        // 设置编码  
        format.setEncoding("UTF-8");  
        // 创建XMLWriter对象,指定了写出文件及编码格式  
        // XMLWriter writer = new XMLWriter(new FileWriter(new  
        // File("src//a.xml")),format);  
        XMLWriter writer = new XMLWriter(new OutputStreamWriter(  
                new FileOutputStream(new File("src//a.xml")), "UTF-8"), format);  
        // 写入          writer.write(document);  
        // 立即写入          writer.flush();  
        // 关闭操作          writer.close();  
    }  
  
    /** 
     * 遍历当前节点元素下面的所有(元素的)子节点 
     *  
     * @param node 
     */  
    public void listNodes(Element node) {  
        System.out.println("当前节点的名称::" + node.getName());  
        // 获取当前节点的所有属性节点  
        List<Attribute> list = node.attributes();  
        // 遍历属性节点  
        for (Attribute attr : list) {  
            System.out.println(attr.getText() + "-----" + attr.getName()  
                    + "---" + attr.getValue());  
        }  
  
        if (!(node.getTextTrim().equals(""))) {  
            System.out.println("文本内容::::" + node.getText());  
        }  
  
        // 当前节点下面子节点迭代器  
        Iterator<Element> it = node.elementIterator();  
        // 遍历  
        while (it.hasNext()) {  
            // 获取某个子节点对象  
            Element e = it.next();  
            // 对子节点进行遍历              listNodes(e);  
        }  
    }  
  
    /** 
     * 介绍Element中的element方法和elements方法的使用 
     *  
     * @param node 
     */  
    public void elementMethod(Element node) {  
        // 获取node节点中,子节点的元素名称为西游记的元素节点。  
        Element e = node.element("西游记");  
        // 获取西游记元素节点中,子节点为作者的元素节点(可以看到只能获取第一个作者元素节点)  
        Element author = e.element("作者");  
  
        System.out.println(e.getName() + "----" + author.getText());  
  
        // 获取西游记这个元素节点 中,所有子节点名称为作者元素的节点 。    
        List<Element> authors = e.elements("作者");  
        for (Element aut : authors) {  
            System.out.println(aut.getText());  
        }  
  
        // 获取西游记这个元素节点 所有元素的子节点。  
        List<Element> elements = e.elements();  
  
        for (Element el : elements) {  
            System.out.println(el.getText());  
        }  
  
    }  
  
}

Appropriately comment part of the code to observe the running effect, and practice repeatedly. I hope you will have a better understanding of dom4j.

7. String and XML mutual conversion case

package dom4j;  
  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.OutputStreamWriter;  
  
import org.dom4j.Document;  
import org.dom4j.DocumentHelper;  
import org.dom4j.Element;  
import org.dom4j.io.OutputFormat;  
import org.dom4j.io.SAXReader;  
import org.dom4j.io.XMLWriter;  
import org.junit.Test;  
  
public class Demo02 {  
  
    @Test  
    public void test() throws Exception {  
  
        // 创建saxreader对象  
        SAXReader reader = new SAXReader();  
        // 读取一个文件,把这个文件转换成Document对象  
        Document document = reader.read(new File("src//c.xml"));  
        // 获取根元素  
        Element root = document.getRootElement();  
        // 把文档转换字符串  
        String docXmlText = document.asXML();  
        System.out.println(docXmlText);  
        System.out.println("---------------------------");  
        // csdn元素标签根转换的内容  
        String rootXmlText = root.asXML();  
        System.out.println(rootXmlText);  
        System.out.println("---------------------------");  
        // 获取java元素标签 内的内容  
        Element e = root.element("java");  
        System.out.println(e.asXML());  
  
    }  
  
    /** 
     * 创建一个document对象 往document对象中添加节点元素 转存为xml文件 
     *  
     * @throws Exception 
     */  
    public void test2() throws Exception {  
  
        Document document = DocumentHelper.createDocument();// 创建根节点  
        Element root = document.addElement("csdn");  
        Element java = root.addElement("java");  
        java.setText("java班");  
        Element ios = root.addElement("ios");  
        ios.setText("ios班");  
  
        writer(document);  
    }  
  
    /** 
     * 把一个文本字符串转换Document对象 
     *  
     * @throws Exception 
     */  
    public void test1() throws Exception {  
        String text = "<csdn><java>Java班</java><net>Net班</net></csdn>";  
        Document document = DocumentHelper.parseText(text);  
        Element e = document.getRootElement();  
        System.out.println(e.getName());  
        writer(document);  
    }  
  
    /** 
     * 把document对象写入新的文件 
     *  
     * @param document 
     * @throws Exception 
     */  
    public void writer(Document document) throws Exception {  
        // 紧凑的格式  
        // OutputFormat format = OutputFormat.createCompactFormat();  
        // 排版缩进的格式  
        OutputFormat format = OutputFormat.createPrettyPrint();  
        // 设置编码  
        format.setEncoding("UTF-8");  
        // 创建XMLWriter对象,指定了写出文件及编码格式  
        // XMLWriter writer = new XMLWriter(new FileWriter(new  
        // File("src//a.xml")),format);  
        XMLWriter writer = new XMLWriter(new OutputStreamWriter(  
                new FileOutputStream(new File("src//c.xml")), "UTF-8"), format);  
        // 写入          writer.write(document);  
        // 立即写入          writer.flush();  
        // 关闭操作          writer.close();  
    }  
}

control + Left mouse button

F3 Jump directly to the relevant class

F2 View class details

control + o Display all constructors of the current class

debug:

F5 Enter the processing function F6 Next step F8 Go directly to the next breakpoint

Detailed introduction to the basics of xml parsing java

package cn.jiemoxiaodi.juit;import org.junit.Test;/*
 *junit  使用步骤
 *1、规范问题    类名为 被测试的类名+test  方法名为test+被测试的方法名
 *2、导包   juit
 *3、测试方法必须是  1、方法必须是 public  2、方法返回值是void 3、必须用注解 @Test 
 */public class ToolTest {
    
    
    @Test    public void testAdd() {
        Tool tool=new Tool();        
        int result=tool.add(2, 3);
        System.out.println(result);
    }
}

Detailed introduction to the basics of xml parsing java

The above is the detailed content of Detailed introduction to the basics of xml parsing java. 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