hello.xml
dtd文件是约束文件,用来定义xml文件的,这种写法是写在一块了,但是往往都是分开写的。
记住,xml里面的元素每个都要定义!!!
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE persons [
<!ELEMENT persons (person+)>
<!ELEMENT person (name,password?,age)>
<!--常用的控制符 ?表示0次或者1次,*表示0次或者多次,+表示1次或者多次,默认表示1次-->
<!ATTLIST person sex CDATA #REQUIRED>
<!ATTLIST person id ID #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT password (#PCDATA)>
<!ELEMENT age (#PCDATA)>
]>
<persons>
<person id="p1" sex="男">
<name>zhangsan</name>
<password>123123</password>
<age>12</age>
</person>
<person id="p2" sex="女">
<name></name>
<password></password>
<age></age>
</person>
</persons>
ELEMENT 是元素的意思
ATTLIST 是属性的意思
分开写
persons.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT persons (person+)>
<!ELEMENT person (sex,name,age,messages+)>
<!ATTLIST person id ID #REQUIRED>
<!ELEMENT sex (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT messages (message+)>
<!ELEMENT message (title,content)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT content (#PCDATA)>
persons.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE persons SYSTEM "persons.dtd"> //引入
<persons>
<person id="_1">
<sex/>
<name/>
<age/>
<messages>
<message>
<title/>
<content/>
</message>
</messages>
</person>
<person id="_2">
<sex></sex>
<name></name>
<age></age>
<messages>
<message>
<title></title>
<content></content>
</message>
</messages>
</person>
</persons>
SYSTEM 本地用 PUBLIC 外部文件用
classroom.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT classroom (claName,grade,students)>
<!ATTLIST classroom id ID #REQUIRED>
<!ELEMENT claName (#PCDATA)>
<!ELEMENT grade (#PCDATA)>
<!ELEMENT students (student+)>
<!ELEMENT student (id,stuName,age)>
<!ELEMENT id (#PCDATA)>
<!ELEMENT stuName (#PCDATA)>
<!ELEMENT age (#PCDATA)>
classroom.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE classroom SYSTEM "classroom.dtd">
<classroom id="c1">
<claName>10计算机应用技术</claName>
<grade>2010</grade>
<students>
<student>
<id>1</id>
<stuName>zhangsan</stuName>
<age>12</age>
</student>
<student>
<id>2</id>
<stuName>lisi</stuName>
<age>122</age>
</student>
</students>
</classroom>