hooyantsing's Blog

第53次课程

字数统计: 1.1k阅读时长: 5 min
2019/09/03

源辰74班

第53次课程

09.03.2019

内容

XML简介及DTD[蒋斌]

1.初步了解

Demo.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8" ?>
<students>
    <student>
       <name>张三</name>
       <age>18</age>
       <sex>男</sex>
    </student>
    <student>
       <name>李四</name>
       <age>18</age>
       <sex>女</sex>
    </student>
</students>

2.内部约束

innerDtd.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD内部约束 -->
<!DOCTYPE students[
<!ELEMENT students (student)>
<!ELEMENT student (name,age,sex)>
]>
<students>
    <student>
       <name>Hooy</name>
       <age>21</age>
       <sex>男</sex>
    </student>
</students>

3.外部约束

outDtd.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD外部约束 -->
<!DOCTYPE students SYSTEM "outDtd.dtd">
<students>
    <student>
       <name>Hooy</name>
       <age>21</age>
       <sex>男</sex>
    </student>
</students>

outDtd.dtd

1
2
3
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT students (student)>
<!ELEMENT student (name,age,sex)>

4.#PCDATA EMPTY ANY

peom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<!-- (#PCDATA) 标记内均为字符
     EMPTY 标记内容为空 可直接将标记写为:<标记名/>
     ANY 允许标记内存在其他标记,如<a></a> -->
<!DOCTYPE peom [
<!ELEMENT peom (author,title,content)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT title EMPTY>
<!ELEMENT content ANY>
]>
<peom>
<author>作者###</author>
<title/>
<content>内容</content>
</peom>

5.+ * !ATTLIST #IMPLIED #REQUIRED CDATA

family.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<!-- + 至少存在一个以上标记
     * 至少存在零个以上标记
     ENPTY 标记内容为空
     !ATTLIST 标记属性
     #IMPLIED 选填
     #REQUIRED 必填
     CDATA 字符串 -->
<!DOCTYPE 家庭 [
<!ELEMENT 家庭 (人+,家电*)>
<!ELEMENT 人 EMPTY>
<!ATTLIST 人 婚姻状态  (single|married|divorced|widowed) #IMPLIED
           性别 (男|女) #REQUIRED
           名字 CDATA #REQUIRED>
]>

6.ID唯一约束 #FIXED固定值

fixed.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<!-- -组合约束-
     ID 唯一约束 文本
     #FIXED 固定值
     直接写参数,如"1000000" 默认值   运行XML文件时,即使没写此标记,也将直接展示出来-->
<!DOCTYPE 家庭 [
<!ELEMENT 家庭 (人+)>
<!ELEMENT 人 EMPTY>
<!ATTLIST 人 身份证 ID #REQUIRED
           姓名 CDATA #REQUIRED
           company CDATA #FIXED "微软"
           money CDATA "1000000">
]>
<家庭>
    <人 身份证="A110" 姓名="张三"/>
    <人 身份证="A111" 姓名="李四" company="微软"/>
</家庭>

7.配置文件

dbconfig.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="UTF-8"?>
<!-- 连接数据库配置文件 -->
<!DOCTYPE dbconfig [
<!ELEMENT dbconfig (db+)>
<!ELEMENT db (dirver,url,user,password)>
<!ELEMENT dirver (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ELEMENT user (#PCDATA)>
<!ELEMENT password (#PCDATA)>
<!ATTLIST db type CDATA #REQUIRED>
]>
<dbconfig>
    <db type="oracle">
       <dirver>oracle.jdbc.driver.OracleDriver</dirver>
       <url>jdbc:oracle:thin:@localhost:1521:orcl</url>
       <user>scott</user>
       <password>tiger</password>
    </db>
    <db type="mysql">
       <dirver>com.mysql.jdbc.Driver</dirver>
       <url>jdbc:mysql://localhost:3306/mysql</url>
       <user>root</user>
       <password>a</password>
    </db>
</dbconfig>

8.XML解析(SAX方式)

SAX.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class SAX extends DefaultHandler{

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        /*获取解析器步骤*/
        //1.创建解析器工厂
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过工厂创建解析器
        SAXParser saxparser = factory.newSAXParser();
        //3.通过解析器创建读取器
        XMLReader xmlReader = saxparser.getXMLReader();
        //4.注册事件处理器(处理函数)
        xmlReader.setContentHandler(new SAX());
        xmlReader.parse(SAX.class.getClassLoader().getResource("xml/dbconfig.xml").getPath());

    }
    
    @Override
    public void startDocument() throws SAXException {
        System.out.println("文档开始");
    }
    //判断 标记属性type值
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        System.out.println(qName+"=="+attributes.getValue("type"));
    }
    //获取 标记中的值
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch,start,length);
        System.out.println(value);
    }
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        System.out.println(qName);
    }
    @Override
    public void endDocument() throws SAXException {
        System.out.println("文档结束");
    }
}
CATALOG