XML解析

作者: Tinyspot | 来源:发表于2022-07-12 08:34 被阅读0次

1. XML

1.1 语法规则

  • 只有一个根元素
  • 元素必须正确嵌套
  • 标签对大小写敏感

1.2 特殊字符

  • 格式 &xx
  • &lt; <
  • &gt; >
  • &amp; &
  • &quot; "
  • &apos; '

1.3 文本区域(CDATA 区)

  • 解决特殊字符问题,特殊字符(比如<和>)
  • <![CDATA[纯文本原样显示]]]>
  • CDATA 里的文本内容,不需要 xml 语法解析

1.4 命名空间 Namespace

  • 在XML中,URL作为 XML 的 Namespace
  • 命名空间的语法 xmlns:[prefix]="url of name",其中xmlns:是必须的属性,prefix是命名空间的别名
  • 不带别名的是默认Namespace,例如:xmlns="url of namespace"
  • xmlns是 XML NameSpace 的缩写

2. 解析 XML

  • DOM - Document Object Model
    • 一次性读取XML, 在内存中表示为树形结构
  • SAX
    • 以流的形式读取XML, 使用事件回调

3. XML Schema

  • XML Schema 描述 XML文档的结构,可定义 XML 格式,是构造的语言的一种
  • 可用来检查该 XML 文档是否符合其要求

3.1 xsi 命名空间的属性

  • xsi:schemaLocation
  • 属性的值由一个或多个URI引用对组成,两个URI之间以空白符分隔(空格和换行均可)
  • 定义了 XML Namespace 和对应的 XSD(Xml Schema Definition)文档的位置的关系

3.2 .xsd文件

  • XSD (XML Schemas Definition)

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..*.*(..))"]]/>]

相关文章

  • iOS解析XML

    2.0 XML解析 2.1 XML简单介绍 (1) XML:可扩展标记语言 (2) XML解析 2.2 XML解析...

  • iOS开发-XML解析

    一、 XML解析 1.1 XML简单介绍 (1) XML:可扩展标记语言 (2) XML解析 1.2 XML解析 ...

  • 3-XML解析

    3.0 XML解析 3.1 XML简单介绍 (1) XML:可扩展标记语言 (2) XML解析 3.2 XML解析...

  • XML、JSON解析

    XML解析 XML_GData解析 JSON解析

  • 05、网络解析

    解析:从事先规定好的格式中提取数据iOS开发常见的解析:XML解析,JSON解析 XML解析 XMl事例: XML...

  • Senior进阶 网络之数据解析之XML解析和JSON解析的用法

    Senior进阶 网络之数据解析之XML解析和JSON解析的用法 XML解析之SAX解析 XML解析之DOM解析 ...

  • XML

    NSXMLParser解析XML GDataXML解析XML

  • XML*

    目录 XML简介 XML基本语法 XML解析 * DOM解析   * DOM解析原理及工具   * DOM4J解析...

  • 面试相关

    1.Json解析与XML解析 Gson解析和FastJson ,FastJson解析效率高 XML解析,SAX解析...

  • 五、Groovy语法(五)json、xml解析

    Groovy数据解析 一、json解析 请求网络数据并解析 二、xml解析 groovy解析xml数据 groov...

网友评论

      本文标题:XML解析

      本文链接:https://www.haomeiwen.com/subject/vjiobrtx.html