xml從入門到精通之dtd文件

hello.xml

dtd文件是約束文件,用來定義xml文件的,這種寫法是寫在一塊了,可是每每都是分開寫的。markdown

記住,xml裏面的元素每一個都要定義!!!ide

<?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 是屬性的意思atom

分開寫

persons.dtdspa

<?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.xmlcode

<?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 外部文件用xml

cla***oom.dtdit

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT cla***oom (claName,grade,students)>
<!ATTLIST cla***oom 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)>

cla***oom.xmlio

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cla***oom SYSTEM "cla***oom.dtd">
<cla***oom 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>
</cla***oom>
相關文章
相關標籤/搜索