XML简介之DTD约束的相关实例

103 篇文章 2 订阅
79 篇文章 0 订阅

这是一个关于Book的DTD约束文件

Book.dtd

<!ELEMENT book (bookname,bookprice,author)>

<!ELEMENT bookname (#PCDATA)>
<!ELEMENT bookprice (#PCDATA)>
<!ELEMENT author (#PCDATA)>      

在外部引用该约束的文件

Book.xml

<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?>
<!DOCTYPE bookshelf SYSTEM  "Book.dtd">
<book>
<bookname>java程序设计</bookname>
<bookprice>30元</bookprice>
<author>lijizh1013</author>
</book>

再来看一个将约束写到xml文件中的电视时间表的实例:

<?xml version="1.0" encoding="UTF-8"?>

//约束声明部分
<!DOCTYPE TVSCHEDULE [

<!ELEMENT TVSCHEDULE (CHANNEL+)>
<!ELEMENT CHANNEL (BANNER,DAY+)>
<!ELEMENT BANNER (#PCDATA)>
<!ELEMENT DAY (DATE,(HOLIDAY|PROGRAMSLOT+)+)>
<!ELEMENT HOLIDAY (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT PROGRAMSLOT (TIME,TITLE,DESCRIPTION?)>
<!ELEMENT TIME (#PCDATA)>
<!ELEMENT TITLE (#PCDATA)>
<!ELEMENT DESCRIPTION (#PCDATA)>

<!ATTLIST TVSCHEDULE NAME CDATA #REQUIRED>
<!ATTLIST CHANNEL CHAN CDATA #REQUIRED>
<!ATTLIST PROGRAMSLOT VTR CDATA #IMPLIED>
<!ATTLIST TITLE RATING CDATA #IMPLIED>
<!ATTLIST TITLE LANGUAGE CDATA #IMPLIED>

]>
//正式的xml文件部分

<TVSCHEDULE NAME = "LIJIZH">
<CHANNEL CHAN = "1013">
<BANNER>A</BANNER>
<DAY>
<DATE>1991-10-13</DATE>
<HOLIDAY>100 DAY</HOLIDAY>
</DAY>
</CHANNEL>
</TVSCHEDULE>

其他相关实例:

一个新闻报纸的实例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE NEWSPAPER [

<!ELEMENT NEWSPAPER (ARTICLE+)>
<!ELEMENT ARTICLE (HEADLINE,BYLINE,LEAD,BODY,NOTES)>
<!ELEMENT HEADLINE (#PCDATA)>
<!ELEMENT BYLINE (#PCDATA)>
<!ELEMENT LEAD (#PCDATA)>
<!ELEMENT BODY (#PCDATA)>
<!ELEMENT NOTES (#PCDATA)>

<!ATTLIST ARTICLE AUTHOR CDATA #REQUIRED>
<!ATTLIST ARTICLE EDITOR CDATA #IMPLIED>
<!ATTLIST ARTICLE DATE CDATA #IMPLIED>
<!ATTLIST ARTICLE EDITION CDATA #IMPLIED>

<!ENTITY NEWSPAPER "Vervet Logic Times">
<!ENTITY PUBLISHER "Vervet Logic Press">
<!ENTITY COPYRIGHT "Copyright 1998 Vervet Logic Press">

]>
<NEWSPAPER>
<ARTICLE AUTHOR = "lijizh">
<HEADLINE>STUDYING</HEADLINE>
<BYLINE>1990-10-12</BYLINE>
<LEAD>lijizh</LEAD>
<BODY>This is a pare of newspaper!</BODY>
<NOTES>Please look at the newspaper!</NOTES>
</ARTICLE>
</NEWSPAPER>

一个关于国家从中央到地方的最高长官的名称的实例:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE country [
   <!ELEMENT country (countries+)>
   <!ELEMENT countries (province+)>
   <!ELEMENT province (city+)>
   <!ELEMENT city (countryside+)>
   
   <!ATTLIST countries name CDATA #REQUIRED>
   <!ATTLIST province
    name CDATA #REQUIRED
   stadholder CDATA #REQUIRED>   
   <!ATTLIST city
     name CDATA #REQUIRED
   mayor CDATA #REQUIRED>
   <!ATTLIST countryside  
     name CDATA #REQUIRED
   alcalde CDATA #REQUIRED>
   
]>
<country>
  <countries name = "China">
     <province  name ="HeBeiSheng" stadholder = "张三">
 
          <city name = "BaoDingShi" mayor = "李四">
                <countryside name ="BaiGouZhen" alcalde = "王五">
                            lijizh
                </countryside>
          </city>
          
    </province>
    <province name ="HeNanSheng" stadholder = "马六">
 
          <city name = "KaiFengShi" mayor = "橫七">
                <countryside name = "GaoYangZhen" alcalde = "竖八">
                            lijizh
                </countryside>
          </city>
          
    </province>
    

</countries>
</country>


其实对于我们这些Java程序员来说,我们只要能够读懂DTD的约束文件,并且能够将简单的约束文件中的信息反映到我们的xml文件中就已经达到学习的目的了,因为,我们主要使用xml文件写配置文件的,另外,由于DTD约束存在一些弊端,所以在以后我们还可能会用到Schema约束来代替DTD约束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值