XML

XML 指可扩展标记语言(eXtensible Markup Language)。XML 被设计用来传输和存储数据。

一、XML
1、XML声明
文件第一行,固定格式:

<?xml version="1.0" encoding="字符编码集"?>

2、XML语法

(1)所有 XML 元素都须有关闭标签
(2)XML 标签对大小写敏感
(3)XML 必须正确地嵌套
(4)XML 文档必须有根元素
(5)XML 的属性值须加引号
(6)实体引用
在 XML 中,有 5 个预定义的实体引用:
&lt; < 小于
&gt; > 大于
&amp; & 和号
&apos; ' 单引号
&quot; " 引号
(7)XML 中的注释
<!-- This is a comment -->
(8)在 XML 中,空格会被保留,HTML 会把多个连续的空格字符裁减(合并)为一个
(9)XML 以 LF 存储换行
在 Windows 应用程序中,换行通常以一对字符来存储:回车符 (CR) 和换行符 (LF)。这对字符与打字机设置新行的动作有相似之处。在 Unix 应用程序中,新行以 LF 字符存储。而 Macintosh 应用程序使用 CR 来存储新行。

3、验证XML是否合法的两种方式:
(1)DTD
(2)XML Schema,DTD 代替者

二、DTD-文档类型定义
DTD(文档类型定义)的作用是定义 XML 文档的合法构建模块。它使用一系列的合法元素来定义文档结构。

1、DOCTYPE 声明
(1)使用内部的dtd文件
格式:<!DOCTYPE 根元素名 [ dtd内容 ]>

(2)引入外部的dtd文件
格式:<!DOCTYPE 根元素名 STSTEM "dtd文件的路径">

(3)使用网络上的dtd文件 
格式:<!DOCTYPE 根元素名 PUBLIC "dtd名称" "dtd的URL">

2、DTD构建

(1)构建模块
(2)元素
(3)属性
(4)实体

三、Schema
XML Schema 是基于 XML 的 DTD 替代者。XML Schema 描述 XML 文档的结构。XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD)

1、Schema优势
XML Schema 支持数据类型

2、在 XML 文档中引用 Schema
使用spring的配置文件来举例说明:

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

(1)片段:xmlns="http://www.springframework.org/schema/beans" 声明默认命名空间;
(2)xmlns:mvc、xmlns:tx、 xmlns:aop、xmlns:context 声明引入多个schema文件,和xmlns=""作用是一样的,用于声明其他引入的schema文件的命名空间
需要使用别名xmlns:alias,指定对应的命名空间,该命名空间应该和下面的xsi:schemaLocation相对应命名空间相对应,以此来查找模式位置;
(3)xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 声明XML Schema实例名称空间,只有声明了xmlns:xsi,才能使用xsi:schemaLocation;
(4)xsi:schemaLocation=""指定命名空间和模式位置,成对出现,可以配置多对,要与上面引入的多个schema文件命名空间相对应;


参考:
XML 系列教程
XML 教程

Spring中xml文档的schema约束

posted @ 2020-06-10 11:50  cac2020  阅读(140)  评论(0编辑  收藏  举报