XML解析文档解析

1.首先是我的项目结构以及我所引入的依赖:

XML解析文档解析_第1张图片

2.引入的依赖:jdk用的是17


    17
    17


    
        dom4j
        dom4j
        1.6.1
    
    
        org.projectlombok
        lombok
    
    
    
        jaxen
        jaxen
    
    
        junit
        junit
        test
    

3.测试代码如下:

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);
        Element ele = (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 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 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 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.说说每一个方法用来做什么,具体的作用:

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

0.SAX解析器读取位于类路径下的名为"mybatis-conf.xml"的XML文件,并将其解析为一个Document对象。其中,SAXReader是一个SAX解析器,
InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("mybatis-conf.xml")用于获取mybatis-conf.xml文件的输入流,
Document read = saxReader.read(resourceAsStream)则是将输入流解析为一个Document对象:

1. Element  node =(Element) read.selectSingleNode(xPath);:根据XPath表达式获取mybatis-conf.xml文件中的根节点,即节点。

2. String node1 = node.attributeValue("default");:获取节点的default属性值,该属性值用于指定默认的环境(即数据库)。

3. String xPath1="/configuration/environments/environment[@id='"+node1+"']";:根据default属性值构造XPath表达式,用于获取指定id的节点。

4. Element ele = (Element) read.selectSingleNode(xPath1);:根据XPath表达式获取指定id的节点。

5. Element transactionManager = ele.element("transactionManager");:获取节点下的节点。

6. String transactionManager1 = transactionManager.attributeValue("type");:获取节点的type属性值,即事务管理器的类型。

7. Element dataSource = ele.element("dataSource");:获取节点下的节点,即数据库连接池配置信息。

8. String pooled = dataSource.attributeValue("type");:获取节点的type属性值,即数据库连接池的类型。

9. List elements = dataSource.elements();:获取节点下的所有子节点,即数据库连接池的配置信息。

10. elements.forEach(elem->{}:遍历数据库连接池的配置信息,获取每个子节点的name和value属性值。

11. String xpaht2="//mapper";:构造XPath表达式,用于获取所有节点。

12. List list = read.selectNodes(xpaht2);:根据XPath表达式获取所有节点。

13. for (Element l:list) {}:遍历所有节点,获取每个节点的resource属性值,即mapper文件的路径。

你可能感兴趣的:(java)