XML配置文件说明

1.DTD(Documnet Type Definition)

DTD即文档类型定义,是一种XML约束模式语言,是XML文件的验证机制,属于XML文件组成的一部分。
一个 DTD文档包含:元素的定义规则,元素间关系的定义规则,元素可使用的属性,可使用的实体或符号规则。

DTD和XSD相比:DTD 是使用非 XML 语法编写的。
DTD 不可扩展,不支持命名空间,只提供非常有限的数据类型 .
DTD 现在基本已被XSD文档取代,但是,仍有个别在使用 比如 Mybatis mapper xml文件

<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
   	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
SYSTEM表示本地 PUBLC 表示网络

上面用的是公共DTD,DTD名称格式为"注册//组织//类型 标签//语言"
“注册” 指示组织是否由国际标准化组织(ISO)注册,+表示是,-表示不是;
“组织” 即组织名称,如:mybatis.org;
“类型” 一般是DTD;
“标签” 是指定公开文本描述,即对所引用的公开文本的唯一描述性名称,后面可附带版本号,如Mapper 3.0。
“语言” EN指英语;
http://mybatis.org/dtd/mybatis-3-mapper.dtd 表示外部DTD文件URI

DTD基本语法:<!ELEMENT NAME CONTENT>

其中:

  • ELEMENT是关键字,是不能修改的
  • NAME表示元素名称
  • CONTENT是元素类型,必须要大写!

 <!ELEMENT 班级 (学生+)>
<!ELEMENT 学生 (名字,年龄,介绍)>
<!ELEMENT 名字 (#PCDATA)>                                     一个.dtd文件
<!ELEMENT 年龄 (#PCDATA)>
<!ELEMENT 介绍 (#PCDATA)>

使用DTD验证模式需要在XML文件的头部声明

 <?xml version="1.0" encoding="utf-8"?>
 <!-- 引入dtd文件,约束这个xml -->
 <!DOCTYPE 班级 SYSTEM "myclass.dtd">
 <班级>	
      <学生>		
           <名字>小红</名字>		
           <年龄>10</年龄>		
           <介绍>学习刻苦</介绍>	
      </学生>
 </班级>

2.XSD(XML Schemas Definition)

XML Schema语言也就是XSD。XML Schema描述了XML文档的结构。检查该XML文档是否符合其要求。

XSD是DTD替代者的原因,一是据将来的条件可扩展,二是比DTD丰富和有用,三是用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:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    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">

首先说明最基本的头部命名空间信息,配置文件必须的部分,固定部分 xmlns=“http://www.springframework.org/schema/beans” 声明xml文件默认的命名空间,表示未使用其他命名空间的所有标签的默认命名空间。
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” 声明XML Schema 实例,声明后就可以使用 schemaLocation属性了。
xmlns:context=“http://www.springframework.org/schema/context” 这是spring配置文件里面需要使用到context的标签,声明前缀为context的命名空间,在容器启动的时候找到对应的命名空间处理器处理。当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。
xsi:schemaLaction部分: http://www.springframework.org/schema/beans — 表示区别命名空间url http://www.springframework.org/schema/beans/spring-beans.xsd — 表示命名空间对应xsd文件获取地址(也只是一个映射地址)。

  • 容器启动时按照此顺序查找

在spring-webmvc.jar包中找到META_INF/spring.handlers

http\://www.springframework.org/schema/mvc=org.springframework.web.servlet.config.MvcNamespaceHandler

META_INF/spring.schemas

http\://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd=org/springframework/web/servlet/config/spring-mvc-3.1.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd=org/springframework/web/servlet/config/spring-mvc-3.2.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd=org/springframework/web/servlet/config/spring-mvc-4.0.xsd
http\://www.springframework.org/schema/mvc/spring-mvc.xsd=org/springframework/web/servlet/config/spring-mvc-4.0.xsd

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值