Java USES dom4j to manipulate the XML sample code

  • 2020-04-01 02:32:00
  • OfStack

Dom4j is an excellent Java XML API with excellent performance, power, and extreme ease of use, as well as an open source tool. Can be downloaded in this address http://dom4j.sourceforge.net.
The dom4j we use here is the dom4j-1.6.1 version, we only need to use the following two jars:


dom4j-1.6.1.jar
commons-io-2.4.jar

1. Dom4j reads XML strings


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class TestReadXMLString {
    public static void main(String[] args) throws DocumentException {
        String readline = "<?xml version="1.0" encoding="utf-8"?><students><student sid="001"> <id>001</id><name> The plane </name> <age>18</age> </student></students>";
        Document document = DocumentHelper.parseText(readline);
        Element rootElm = document.getRootElement();
        System.out.println("rootElement:  " + rootElm.getName());
        Element student = rootElm.element("student");
        Element id = student.element("id");
        Element name = student.element("name");
        Element age = student.element("age");
        System.out.println(id.getText());
        System.out.println(name.getText());
        System.out.println(age.getText());
    }
}

2. Dom4j creates XML files


import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
public class TestWriteXMLString {
    public static void main(String[] args) {
        OutputFormat format = OutputFormat.createPrettyPrint();
        //1. Construct an empty Document
        Document doc = DocumentHelper.createDocument();
        doc.addComment("this is a comment");
        //2. Construct the root element
        Element rootElmt = doc.addElement("users");
        rootElmt.addNamespace("test", "www.test.com");
        Element userElmt = rootElmt.addElement("user");
        userElmt.addAttribute("number", "1001");
        userElmt.addElement("name").setText("zhangsan");
        userElmt.addElement("age").setText("20");
        userElmt.addElement("gender").setText("mail");
        Element userElmt2 = rootElmt.addElement("user");
        userElmt.addAttribute("number", "1002");
        userElmt2.addElement("name").setText("zhangsan");
        userElmt2.addElement("age").setText("20");
        userElmt2.addElement("gender").setText("mail");
        System.out.println(doc.asXML().replaceAll("n", ""));
    }
}

3. Read or write XML files

Read XML file


SAXReader reader = new SAXReader();
String path = "E:/Workspaces/MyEclipse 8.6/xmltest/file/student.xml";
Document document = reader.read(new File(path));

Write an XML file


OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");//Set the encoding format of the XML file
String filePath = "E:/Workspaces/MyEclipse 8.6/xmltest/file/student.xml";
Document document = DocumentHelper.createDocument();
doc.addComment("this is a comment");

/ create document content


XMLWriter writer = new XMLWriter(new FileWriter(filePath), format);//Writes to the specified file
writer.write(document);
 writer.close();


Related articles: