XML解析

2022-07-12  本文已影响0人  Tinyspot

1. XML

1.1 语法规则

1.2 特殊字符

1.3 文本区域(CDATA 区)

1.4 命名空间 Namespace

2. 解析 XML

3. XML Schema

3.1 xsi 命名空间的属性

3.2 .xsd文件

4. Dom4j 解析 XML

public static void main(String[] args) throws DocumentException, FileNotFoundException {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("bean.xml");
        // 等价
        InputStream inputStream1 = new FileInputStream(new File(Thread.currentThread().getContextClassLoader().getResource("applicationContext-back.xml").getFile()));
        InputStream inputStream2 = new FileInputStream(Thread.currentThread().getContextClassLoader().getResource("applicationContext-back.xml").getFile());

        // SAXReader 就是一个管道,用一个流的方式,把 xml 文件读出来
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputStream);

        // 获取整个文档,rootElement 包含整个 xml 文档的内容
        Element rootElement = document.getRootElement();
        Iterator<Element> iterator = rootElement.elementIterator();
        while (iterator.hasNext()) {
            Element element = iterator.next();
            String namespaceUri = element.getNamespaceURI();
            String elementName = element.getName();

            if ("http://www.springframework.org/schema/beans".equals(namespaceUri)) {
                // 未带前缀的都属于 beans_namespace, 例如 <bean> 与 <import>
                if ("bean".equals(elementName)) {
                    String beanName = element.attributeValue("id");
                    String classValue = element.attributeValue("class");
                    System.out.println("===bean-info: " + beanName + ";" + classValue);
                } else if ("import".equals(elementName)) {
                    String resource = element.attributeValue("resource");
                    System.out.println("===import-info: " + resource);
                }
            } else if ("http://www.springframework.org/schema/aop".equals(namespaceUri)) {
                List<Element> elements = element.elements();
                for (Element aopElement : elements) {
                    String name = aopElement.getName();
                    if ("aspect".equals(name)) {
                        String ref = aopElement.attributeValue("ref");
                        List<Element> aspectElements = aopElement.elements();
                        for (Element aspectElement : aspectElements) {
                            // 解析 before, after ...
                            String childName = aspectElement.getName();
                            if ("before".equals(childName) || "after".equals(childName) || "after-returning".equals(childName)) {
                                String method = aspectElement.attributeValue("method");
                                String pointcutRef = aspectElement.attributeValue("pointcut-ref");
                                System.out.println("---aop-info: " + childName + ";" + method + ";" + pointcutRef);
                            } else if ("pointcut".equals(childName)) {
                                String idValue = aspectElement.attributeValue("id");
                                String expressionValue = aspectElement.attributeValue("expression");
                                System.out.println("---aop-pointcut-info: " + idValue + ";" + expressionValue);
                            }
                        }
                        // pointcut 可不做遍历,直接获取
                        // Element pointcutElement = aopElement.element("pointcut");
                        // String idValue = pointcutElement.attributeValue("id");
                        // String expressionValue = pointcutElement.attributeValue("expression");
                        // System.out.println("---aop-pointcut-info: " + idValue + ";" + expressionValue);
                    }
                }
            }
        }
    }

xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 带前缀的叫自定义标签 -->
    <context:component-scan base-package="com.example.concrete"/>
    <context:annotation-config/>
    <context:property-placeholder location="classpath:config/config.properties"/>

    <import resource="applicationContext-back.xml" />
    <bean id="userDao" class="com.example.concrete.service.impl.UserDao" />
    <aop:config>
        <aop:aspect ref="userDao">
            <aop:pointcut id="logPoint" expression="execution(* com.example.concrete.service..*.*(..))"/>
            <aop:before method="query" pointcut-ref="logPoint" />
        </aop:aspect>
    </aop:config>

    <bean id="" class="com..." name="alias" autowire="byName">
        <property name="age" name="20"/>
        <property name="userServicer" ref="userServiceIml"/>
    </bean>
</beans>
/**
 * 元素
 * 节点 <bean id="log" class="com.example.concrete.aop.Log" />
 * 节点的属性 id, class
 * 节点的属性值 "log" "com.example.concrete.aop.Log"
 */
org.dom4j.tree.DefaultElement@553f17c 
[Element: <aop:pointcut uri: http://www.springframework.org/schema/aop 
attributes: 
[org.dom4j.tree.DefaultAttribute@4f7d0008 [Attribute: name id value "logPoint"], 
org.dom4j.tree.DefaultAttribute@271053e1 [Attribute: name expression value "execution(* com.example.concrete.service..*.*(..))"]]/>]
上一篇下一篇

猜你喜欢

热点阅读