XML parsing document parsing

1. First is my project structure and the dependencies I introduced:

2. Introduced dependencies: jdk uses 17

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <!--Do xml parsing-->
    <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3. The test code is as follows:

public class TestMybatis {

    @Test
    public void testPorerXml() throws DocumentException {
        SAXReader saxReader=new SAXReader();
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-conf.xml");
        Document read = saxReader.read(resourceAsStream);
        System.out.println("read = " + read);
        String xPath="/configuration/environments";
          Element node =(Element) read.selectSingleNode(xPath);
        String node1 = node.attributeValue("default");
        System.out.println("node1 = " + node1);
        String xPath1="/configuration/environments/environment[@id='" + node1 + "']";
        System.out.println("xPath1 = " + xPath1);
        Elementele = (Element) read.selectSingleNode(xPath1);
        System.out.println("ele = " + ele);
        Element transactionManager = ele.element("transactionManager");
        String transactionManager1 = transactionManager.attributeValue("type");
        System.out.println("transactionManager1 = " + transactionManager1);
        Element dataSource = ele.element("dataSource");
        String pooled = dataSource.attributeValue("type");
        System.out.println("pooled = " + pooled);
        List<Element> elements = dataSource.elements();
        elements.forEach(elem->{
            String name = elem.attributeValue("name");
            String value = elem.attributeValue("value");
            System.out.println("name = " + name + "-----" + "value = " + value);
        });

        String xpaht2="//mapper";
        List<Element> list = read.selectNodes(xpaht2);
        for (Element l:list) {
            System.out.println("l = " + l);
            String resource = l.attributeValue("resource");
            System.out.println("resource = " + resource);
        }
    }


    @Test
    public void testAdminXml() throws DocumentException {
        SAXReader saxReader=new SAXReader();
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("AdminMapper.xml");
        Document document = saxReader.read(resourceAsStream);
        String path="/mapper";
        Element element = (Element) document.selectSingleNode(path);
        String namespace = element.attributeValue("namespace");
        System.out.println("namespace = " + namespace);
        List<Element> elements = element.elements();
        elements.forEach(elem->{
            String id = elem.attributeValue("id");
            System.out.println("id = " + id);
            String useGeneratedKeys = elem.attributeValue("useGeneratedKeys");
            System.out.println("useGeneratedKeys = " + useGeneratedKeys);
            String keyProperty = elem.attributeValue("keyProperty");
            System.out.println("keyProperty = " + keyProperty);
            String resultType = elem.attributeValue("resultType");
            System.out.println("resultType = " + resultType);
            String resultMapper = elem.attributeValue("resultMap");
            System.out.println("resultMapper = " + resultMapper);
            String textTrim = elem.getTextTrim();
            System.out.println("textTrim = " + textTrim);
            String s = textTrim.replaceAll("#\{[0-9A-Za-z_$]*}", "?");
            System.out.println("s = " + s);
        });

    }
}

4. Talk about what each method is used for and its specific function:

SAXReader saxReader=new SAXReader();
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-conf.xml");
Document read = saxReader.read(resourceAsStream);

0. The SAX parser reads the XML file named "mybatis-conf.xml" located on the classpath and parses it into a Document object. Among them, SAXReader is a SAX parser,
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-conf.xml") is used to obtain the input stream of the mybatis-conf.xml file,
Document read = saxReader.read(resourceAsStream) parses the input stream into a Document object: 

1. Element node =(Element) read.selectSingleNode(xPath);: Get the root node in the mybatis-conf.xml file based on the XPath expression, which is the node.

2. String node1 = node.attributeValue(“default”);: Get the default attribute value of the node, which is used to specify the default environment (i.e. database).

3. String xPath1=”/configuration/environments/environment[@id='” + node1 + “‘]”;: Constructs an XPath expression based on the default attribute value, used to obtain the Node.

4. Element ele = (Element) read.selectSingleNode(xPath1);: Get the node with the specified id based on the XPath expression.

5. Element transactionManager = ele.element(“transactionManager”);: Get the node under the node.

6. String transactionManager1 = transactionManager.attributeValue(“type”);: Get the type attribute value of the node, that is, the type of transaction manager.

7. Element dataSource = ele.element(“dataSource”);: Get the node under the node, that is, the database connection pool configuration information.

8. String pooled = dataSource.attributeValue(“type”);: Get the type attribute value of the node, which is the type of the database connection pool.

9. List elements = dataSource.elements();: Get all child nodes under the node, that is, the configuration information of the database connection pool.

10. elements.forEach(elem->{}: Traverse the configuration information of the database connection pool and obtain the name and value attributes of each child node.

11. String xpaht2=”//mapper”;: Constructs an XPath expression to obtain all nodes.

12. List list = read.selectNodes(xpaht2);: Get all nodes based on XPath expression.

13. for (Element l:list) {}: Traverse all nodes and obtain the resource attribute value of each node, that is, the path to the mapper file.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 139228 people are learning the system