dtd的全称是Document Type Definition文档格式定义。是用来约束XML文件格式。
写一个班级的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)>
xml编写时可以这么写:
代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE classroom SYSTEM "classroom.dtd">
<classroom id="class1">
<claName>计算机071-2</claName>
<grade>2011</grade>
<students>
<student>
<id>1</id>
<stuName>张三</stuName>
<age>14</age>
</student>
</students>
</classroom>
dtd文件用的已经比较少了。现在用的比较多的是schema,编写起来比较简单。