Skip to content

Commit dd7d2cc

Browse files
xml基础知识
编写dtd验证xml
1 parent 58c50e6 commit dd7d2cc

File tree

5 files changed

+207
-3
lines changed

5 files changed

+207
-3
lines changed

.idea/workspace.xml

+33-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+75-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,89 @@
11
# mybatis-test
22
**mybatis源码研究-2019/12/04**
33

4-
***主要参考书籍***
4+
***主要参考资料***
55
> - [x] [MyBatis技术内幕 徐郡明 2017/07](https://pan.baidu.com/s/1-JGtoXADDjQRw5v51np4vA "提取码是fcak")
66
> - [x] [MyBatis 3 源码深度解析 江荣波 2019/10](https://pan.baidu.com/s/1-JGtoXADDjQRw5v51np4vA "最新出版没有电子书")
77
> - [x] [MyBatis从入门到精通 刘增辉 2017/07](https://pan.baidu.com/s/1-JGtoXADDjQRw5v51np4vA "提取码是fcak")
88
> - [x] [深入浅出MyBatis 技术原理与实战 杨开振 2016/09](https://pan.baidu.com/s/1-JGtoXADDjQRw5v51np4vA "提取码是fcak")
9+
> - [x] [xml](https://www.w3school.com.cn/xml/index.asp "W3CSchool")
10+
> - [x] [dtd](https://www.w3school.com.cn/dtd/dtd_entities.asp "W3CSchool")
11+
> - [x] [schema](https://www.w3school.com.cn/schema/index.asp "W3CSchool")
12+
> - [x] [xpath](https://www.w3school.com.cn/xpath/xpath_nodes.asp "W3CSchool")
13+
> - [x] [XML入门经典(第5版) 第I部分-->第III部分](https://pan.baidu.com/s/1M3HSfL3VQgpVvHa_ekUjvQ "提取码是9mfm")
14+
> - [x] [疯狂XML讲义](https://pan.baidu.com/s/1M3HSfL3VQgpVvHa_ekUjvQ "提取码是9mfm")
15+
> - [x] [XML揭秘 入门·应用·精通 Michael Morrison 陆新年](https://pan.baidu.com/s/1M3HSfL3VQgpVvHa_ekUjvQ "提取码是9mfm")
16+
> - [x] [《XML简明教程》2009年清华大学出版社出版 张欣毅](https://www.baidu.com "xml基础简明教程,没找到电子版。")
917
18+
1019
***mybatis整体架构***
1120
> mybatis整体架构分为三层,分别是基础支持层、核心处理层和接口层。
1221
> ![mybatis整体架构图](./mybatis整体架构图02.png "mybatis整体架构图")
1322
1423
***mybatis思维导图***
1524
> ![mybatis思维导图](./mybatis整体架构图01.png "mybatis思维导图")
25+
26+
27+
# XML基础知识
28+
29+
## XML
30+
所有的 XML 文档(以及 HTML 文档)均由以下简单的构建模块构成:
31+
32+
> 元素
33+
> 属性
34+
> 实体
35+
> PCDATA
36+
> CDATA
37+
38+
## XML名称空间
39+
名称空间为了避免条目命名冲突
40+
41+
## DTD(Document Type Definition)
42+
43+
文档类型声明或DOCTYPE告诉解析器,XML文档必须遵循DTD定义。同时它也告诉解析器,到哪里找到文档定义的其余内容。
44+
`<!DOCTYPE name [ ]>`
45+
46+
引用外部DTD要用下面两种方法之一:系统标识符和公共标识符。
47+
48+
### 系统标识符:
49+
关键字SYSTEM和指向文档位置的URI引用。URI可以是硬盘上的一个文件,也可以是局域网或者internet上的一个文件。
50+
`<!DOCTYPE name SYSTEM "name.dtd" [...]>`
51+
52+
### 公共标识符:
53+
以PUBLIC关键字开始,其后紧跟一个专用的标识符,但是公共标识不是用来表示对文件的引用,而是表示目录中的一个记录。
54+
根据XML规范,公共标识符可以采用任何格式,但是一种经常使用的格式是正式公共标识符(Formal Public Identifier, FPI)。
55+
> FPI的语法要匹配下面的基本结构:
56+
> -//Owner//Class Description//Language//Version
57+
> 从底层的角度看,它与名称空间的作用相似,但是公共标识符不能把两个不同的词汇组合到同一个文档里。就因为这一点,名称空间比它功能更强大。
58+
59+
在标识符字符串之后,还可以插入一个可选的系统标识符。这样,当处理器不能解析公共标识符时,可以查找这个文档的副本(大多数处理器不能解析公共标识符)。
60+
`<!DOCTYPE name PUBLIC "-//Beginning XML//DTD Name Example//EN" "name.dtd">`
61+
62+
### DTD详述
63+
通常,DTD由三个基本部分组成:
64+
* 元素声明
65+
* 属性声明
66+
* 实体声明
67+
68+
#### 元素声明
69+
由三个部分组成:ELEMENT声明 元素名 元素内容模型
70+
```XML
71+
<!ELEMENT name (first, middle, last)>
72+
```
73+
ELEMENT声明告诉解析器当前声明一个元素。
74+
##### 一个元素内容模型定义了可允许的元素内容。
75+
就XML标准而言,有四类内容文档。(FAQ. 重点,请自行查阅相关资料)
76+
* 元素内容
77+
* 混合内容
78+
* 空内容
79+
* 任意内容
80+
##### 基数是指这个元素在内容模型中出现的次数。DTD有4个基数指示符。
81+
82+
指示符 | 说明
83+
--------|----------------
84+
[none] |内容模型中默认方式,表示这个元素必须且只出现一次
85+
? |表示元素出现一次或零次
86+
* |表示元素出现零次或多次
87+
+ |表示元素出现一次或多次
88+
89+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.pp.mybatis.foundationsupportlayer.parser;
2+
3+
import org.w3c.dom.Document;
4+
import org.w3c.dom.NodeList;
5+
import org.xml.sax.SAXException;
6+
7+
import javax.xml.parsers.DocumentBuilder;
8+
import javax.xml.parsers.DocumentBuilderFactory;
9+
import javax.xml.parsers.ParserConfigurationException;
10+
import javax.xml.xpath.*;
11+
import java.io.IOException;
12+
13+
public class XPathTest {
14+
15+
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
16+
// 参考 javadoc -- javax.xml.parsers.*
17+
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
18+
// 创建的解析器使用DTD验证 如果需要schema验证就指定false 然后使用{@link #setSchema(Schema)}
19+
documentBuilderFactory.setValidating(true);
20+
// 创建的解析器不支持XML命名空间
21+
documentBuilderFactory.setNamespaceAware(false);
22+
// 创建的解析器忽略注释
23+
documentBuilderFactory.setIgnoringComments(true);
24+
// 创建的解析器解析xml文档时是否删除元素内容中的空格
25+
documentBuilderFactory.setIgnoringElementContentWhitespace(false);
26+
// 解析器是否将CDATA节点转换为Text节点,并把它附加到相邻的Text节点
27+
documentBuilderFactory.setCoalescing(false);
28+
// 生成的解析器将扩展实体引用节点
29+
documentBuilderFactory.setExpandEntityReferences(true);
30+
31+
// 创建DocumentBuilder 从而可以获取DOM文档实例
32+
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
33+
// 将文档加载到一个Document对象中
34+
Document doc = builder.parse("src/main/java/org/pp/mybatis/foundationsupportlayer/parser/inventory.xml");
35+
36+
XPathFactory xPathFactory = XPathFactory.newInstance();
37+
// XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历。
38+
// XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointer 都构建于 XPath 表达之上。
39+
// 在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档节点(或称为根节点)。
40+
XPath xPath = xPathFactory.newXPath();
41+
42+
// 编译XPath表达式 --- FAQ 请自己查找答案
43+
XPathExpression expr = xPath.compile("//book[author='Neal Stephenson']/title/text()");
44+
Object result = expr.evaluate(doc, XPathConstants.NODESET/* {@link XPathConstants QName} */);
45+
System.out.println("查询作者位Neal Stephenson的图书的标题:");
46+
NodeList nodeList = (NodeList) result;
47+
printNodeList(nodeList);
48+
49+
System.out.println("查询1997年之后的图书的标题");
50+
nodeList = (NodeList) (xPath.evaluate("//book[@year>1997]/title/text()", doc, XPathConstants.NODESET));
51+
printNodeList(nodeList);
52+
53+
System.out.println("查询1997年之后的图书的属性和标题");
54+
nodeList = (NodeList) (xPath.evaluate("//book[@year>1997]/@*|//book[@year>1997]/title/text()", doc, XPathConstants.NODESET));
55+
printNodeList(nodeList);
56+
57+
}
58+
59+
private static void printNodeList(NodeList nodeList) {
60+
int len = nodeList.getLength();
61+
for (int i = 0; i < len; i++) {
62+
System.out.println(nodeList.item(i).getNodeValue());
63+
}
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!ELEMENT inventory (book*)>
2+
<!ELEMENT book (title, author*, publisher,isbn,price)>
3+
<!ATTLIST book year CDATA #IMPLIED>
4+
<!ELEMENT title (#PCDATA)>
5+
<!ELEMENT author (#PCDATA)>
6+
<!ELEMENT publisher (#PCDATA)>
7+
<!ELEMENT isbn (#PCDATA)>
8+
<!ELEMENT price (#PCDATA)>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!DOCTYPE inventory SYSTEM "inventory.dtd">
3+
<inventory>
4+
<book year="2000">
5+
<title>Snow Crash</title>
6+
<author>Neal Stephenson</author>
7+
<publisher>Spectra</publisher>
8+
<isbn>0553380958</isbn>
9+
<price>14.95</price>
10+
</book>
11+
<book year="2005">
12+
<title>Burning Tower</title>
13+
<author>Larry Niven</author>
14+
<author>Jerry Pournelle</author>
15+
<publisher>Pocket</publisher>
16+
<isbn>0743416910</isbn>
17+
<price>5.99</price>
18+
</book>
19+
<book year="1995">
20+
<title>Zodiac</title>
21+
<author>Neal Stephenson</author>
22+
<publisher>Spectra</publisher>
23+
<isbn>0553573862</isbn>
24+
<price>7.50</price>
25+
</book>
26+
</inventory>

0 commit comments

Comments
 (0)