【php基础】XML,DTD实例
要求一:能看懂别人写的DTD文件,自己能写更好。例如products.dtd:
1 <!ENTITY AUTHOR "John Doe"> 2 <!ENTITY COMPANY "JD Power Tools, Inc."> 3 <!ENTITY EMAIL "[email protected]"> 4 5 <!ELEMENT CATALOG (PRODUCT+)> 6 7 <!ELEMENT PRODUCT 8 (SPECIFICATIONS+,OPTIONS?,PRICE+,NOTES?)> 9 <!ATTLIST PRODUCT 10 NAME CDATA #IMPLIED 11 CATEGORY (HandTool|Table|Shop-Professional) "HandTool" 12 PARTNUM CDATA #IMPLIED 13 PLANT (Pittsburgh|Milwaukee|Chicago) "Chicago" 14 INVENTORY (InStock|Backordered|Discontinued) "InStock"> 15 16 <!ELEMENT SPECIFICATIONS (#PCDATA)> 17 <!ATTLIST SPECIFICATIONS 18 WEIGHT CDATA #IMPLIED 19 POWER CDATA #IMPLIED> 20 21 <!ELEMENT OPTIONS (#PCDATA)> 22 <!ATTLIST OPTIONS 23 FINISH (Metal|Polished|Matte) "Matte" 24 ADAPTER (Included|Optional|NotApplicable) "Included" 25 CASE (HardShell|Soft|NotApplicable) "HardShell"> 26 27 <!ELEMENT PRICE (#PCDATA)> 28 <!ATTLIST PRICE 29 MSRP CDATA #IMPLIED 30 WHOLESALE CDATA #IMPLIED 31 STREET CDATA #IMPLIED 32 SHIPPING CDATA #IMPLIED> 33 34 <!ELEMENT NOTES (#PCDATA)>
要求二:能根据别人写的DTD文件来写符合标准的xml文件,products.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE CATALOG SYSTEM "products.dtd"> 3 <CATALOG> 4 <PRODUCT NAME="冰箱" CATEGORY="HandTool" PLANT="Chicago" INVENTORY="Discontinued"> 5 <SPECIFICATIONS WEIGHT="80kg" POWER="1000w">这是国产海尔冰箱</SPECIFICATIONS> 6 <PRICE>1000</PRICE> 7 <NOTES>轻拿轻放</NOTES> 8 </PRODUCT> 9 </CATALOG>
要求三:可以检验自己写的xml文件是否有误, checkdtd.html
1 <html> 2 <head> 3 <title>测试dtd</title> 4 <meta http-equiv="content-type" content="text/html;charset=utf-8"/> 5 <script language="javascript"> 6 <!-- 7 //创建一个xml解析器 8 var xmldoc = new ActiveXObject("Microsoft.XMLDOM"); 9 //开启校验功能 10 xmldoc.validateOnParse =true; 11 //指定对哪个 xml文件校验 12 xmldoc.load("products.xml"); 13 //如果有错误信息,则输出 14 document.write("错误信息="+xmldoc.parseError.reason+"<br/>"); 15 document.write("错误的行号="+xmldoc.parseError.line+"<br/>"); 16 17 //--> 18 </script> 19 </head> 20 </html>
以上代码写的时候都要注意编码一致为utf-8
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。