Dom4j user manual

Posted by flumpy on Mon, 28 Feb 2022 08:50:59 +0100

1, Use of Dom4j

1. Introduction to XML parsing

There are two common XML parsing methods in daily development

  1. DOM: the parser is required to load the entire XML file into memory and generate a Document object.
    Advantages: the structure and relationship between elements are preserved, and you can add, delete, check and modify elements.
    Disadvantages: if the XML file is too large, it may cause memory overflow.
  2. SAX: it is a faster and more efficient parsing method. It scans line by line, analyzes while scanning, and carries out specific analysis in an event driven way. Each analyzed line will trigger an event.
    Advantages: there will be no memory overflow problem, and large files can be processed.
    Disadvantages: can only read, can not write.
    For the convenience of developers, there are specific ways to parse XML.
    dom4j: a relatively simple class library for XML parsing.
    Jsup: a powerful DOM parsing class library, especially for HTML parsing.

2. Use dom4j to parse XML

java project integration dom4j

  1. Define version
<dom4j.version>2.1.3</dom4j.version>
  1. Add dependency management
<dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>${dom4j.version}</version>
 </dependency>
  1. Add dependency
<dependency>
           <groupId>dom4j</groupId>
           <artifactId>dom4j</artifactId>
</dependency>

Use dom4j to resolve user xml

To parse xml using dom4j

  1. Create parser object
  2. Use the parser object to read the XML Document and generate the Document object
  3. Get the element (label) information of XML according to the Document object

Dom4j important API description

  1. Create parser object
SAXReader saxReader = new SAXReader();
Doucoment document = saxReader.read(Test.class.getClassLoader().getResource("user.xml");//Test.class, user xml: the xml file that needs to be parsed
  1. org. dom4j. Common methods of docunmen
Element getRootElement();//Get root node
String getName();//Returns the name of the label
List<Element> elements();// Gets the child label of the label
String attributeValue(String name);//Gets the specified property
String getText();//Gets the text of the label
String elementText(String name);//Gets the text of the self label with the specified name and returns the value of the sub label text

Conclusion: this method is analyzed layer by layer, and for loops are carried out layer by layer. It is very troublesome and inflexible. So we need to use the following XPath to parse xml.

3. dom4j parsing XML with XPath

XPath can use path expressions to select element or attribute nodes in XML documents. Nodes are selected along the path.
Official document address of XPath: https://zvon.org/xxl/XPathTutorial/General_chi/examples.html

dom4j integration XPath

  1. Define version
<jaxen.version>1.1.1</jaxen.version>
  1. Add dependency management
<dependency>
    <groupId>jaxen</groupId>
    <artifactId>jaxen</artifactId>
    <version>${jaxen.version}</version>
</dependency>
  1. Add dependency
<dependency>
    <groupId>jaxen</groupId>
    <artifactId>jaxen</artifactId>
</dependency>

dom4j provides an XPath based API

Doucument/Element api for XPath

Node selectSingleNode(String xpathExpression);//Get a single label (element / node) according to XPath.
List<Node> selectNodes(String xpathExpression);//Get multiple tags (elements / nodes) according to XPath

Syntax of XPath

  1. Absolute path representation: the path starting with / represents the absolute path. The absolute path is written from the root element.
    For example: element / sub element / sub element

  2. Relative path method: the node that continues to be searched relative to the element of the current node. It does not start with /,, /, indicating the previous element/ Represents the current element.

  3. Full text search path method: / / indicates that no matter how many layers there are in the middle, you can directly obtain the elements that meet the conditions of all child elements.
    /It means to find only one layer.
    For example: / / child element, / / child element / / child element, / / child element / child element.

  4. Form of predicate (condition filtering): for example: / / element [@ attr1 = 'value']

Topics: Java xml server