The XML of sample code is parsed in Java using dom4j
- 2020-04-01 02:23:25
- OfStack
Dom and Sax are two standard ways of parsing that already exist in Java
But it wasn't easy to operate, and for a beginner like me, some of the code was a living sickness
To this end, great third-party development groups have developed tools such as Jdom and Dom4j
Given current trends, we'll cover the basic usage of Dom4j here, without involving complex operations such as recursion
There are many USES of Dom4j, and the examples on the website are a little obscure, so I won't write them here
First we need to create an XML document before we can parse it
XML documents:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="001">
<title>Harry Potter</title>
<author>J K. Rowling</author>
</book>
<book id="002">
<title>Learning XML</title>
<author>Erik T. Ray</author>
</book>
</books>
Example 1: parsing XML in the form of a List
import java.io.File;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
File file = new File("books.xml");
Document document = reader.read(file);
Element root = document.getRootElement();
List<Element> childElements = root.elements();
for (Element child : childElements) {
//If you don't know the property name
//Given the property name
System.out.println("id: " + child.attributeValue("id"));
//Unknown child element name case
//Given the name of the child element
System.out.println("title" + child.elementText("title"));
System.out.println("author" + child.elementText("author"));
//This line exists for aesthetic formatting purposes
System.out.println();
}
}
}
Example 2: parsing XML in the manner of an Iterator Iterator
import java.io.File;
import java.util.Iterator;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Demo {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("books.xml"));
Element root = document.getRootElement();
Iterator it = root.elementIterator();
while (it.hasNext()) {
Element element = (Element) it.next();
//In the case of an unknown property name
//Given the property name
System.out.println("id: " + element.attributeValue("id"));
//Unknown element names
//Given the element name
System.out.println("title: " + element.elementText("title"));
System.out.println("author: " + element.elementText("author"));
System.out.println();
}
}
}
Operation results:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310230947052.png ">
Example 3: create an XML document and output it to a file
import java.io.File;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Demo {
public static void main(String[] args) throws Exception {
Document doc = DocumentHelper.createDocument();
//Add a root node
Element books = doc.addElement("books");
//Add child element
Element book1 = books.addElement("book");
Element title1 = book1.addElement("title");
Element author1 = book1.addElement("author");
Element book2 = books.addElement("book");
Element title2 = book2.addElement("title");
Element author2 = book2.addElement("author");
//Add properties for child nodes
book1.addAttribute("id", "001");
//Add content to the element
title1.setText("Harry Potter");
author1.setText("J K. Rowling");
book2.addAttribute("id", "002");
title2.setText("Learning XML");
author2.setText("Erik T. Ray");
//Instantiate the output format object
OutputFormat format = OutputFormat.createPrettyPrint();
//Set output encoding
format.setEncoding("UTF-8");
//Create the File object to write to
File file = new File("D:" + File.separator + "books.xml");
//Generate the XMLWriter object with the parameters in the constructor as the file stream and format that you want to output
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
//The write method contains the Document object created above
writer.write(doc);
}
}
Operation results:
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310230947053.png ">