可以使用DTD(文档类型定义)来规定xml文档构建模块,可用起对xml文件进行验证。具体用法转:http://www.w3school.com.cn/dtd/dtd_intro.asp。同样也可以用Schema来实现DTD的功能,并且Schema因为其本身可以规定数据类型和其扩展性要优于DTD,并且将逐步取代DTD,具体用法转:http://www.w3school.com.cn/schema/index.asp
例子1:给xml文件编写对应的DTD文件
ex1.xml文件:
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE game SYSTEM "game.dtd"> <!-- Date:2016/1/26 Writer:Wayne Ng Theme:MapleStory hero briefInstruction --> <game> <warrior> <hero> <name>英雄</name> <weapons>剑、斧、钝器</weapons> <skill>终极打击</skill> </hero> <hero> <name>圣骑士</name> <weapons>剑、钝器</weapons> <skill>神圣冲击</skill> </hero> <hero> <name>黑骑士</name> <weapons>长枪、矛</weapons> <skill>黑暗穿刺</skill> </hero> </warrior> <magician> <hero> <name>主教</name> <weapons>长杖、短仗</weapons> <skill>圣光普照</skill> </hero> <hero> <name>火毒法师</name> <weapons>长杖、短仗</weapons> <skill>末日火焰</skill> </hero> <hero> <name>冰雷法师</name> <weapons>长杖、短仗</weapons> <skill>冰咆哮</skill> </hero> </magician> <archer> <hero> <name>神射手</name> <weapons>弓</weapons> <skill>暴风箭雨</skill> </hero> <hero> <name>箭神</name> <weapons>弩</weapons> <skill>终极扫射</skill> </hero> </archer> <ranger> <hero> <name>侠盗</name> <weapons>短剑、短刀</weapons> <skill>暗杀</skill> </hero> <hero> <name>隐士</name> <weapons>拳套</weapons> <skill>四连镖</skill> </hero> <hero> <name>暗影双刀</name> <weapons>短剑、短刀</weapons> <skill>终极斩</skill> </hero> </ranger> </game>
game.dtd文件:
<!ELEMENT warrior (#PCDATA)> <!ELEMENT hero (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT weapons (#PCDATA)> <!ELEMENT skill (#PCDATA)> <!ELEMENT magician (#PCDATA)> <!ELEMENT archer (#PCDATA)> <!ELEMENT ranger (#PCDATA)>
例子2:使用Schema实现例1的功能
ex2.xml文件:
<?xml version = "1.0" encoding = "UTF-8"?> <!-- Date:2016/1/26 Writer:Wayne Ng Theme:MapleStory hero briefInstruction --> <game xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="game.xsd"> <warrior> <hero> <name>英雄</name> <weapons>剑、斧、钝器</weapons> <skill>终极打击</skill> </hero> <hero> <name>圣骑士</name> <weapons>剑、钝器</weapons> <skill>神圣冲击</skill> </hero> <hero> <name>黑骑士</name> <weapons>长枪、矛</weapons> <skill>黑暗穿刺</skill> </hero> </warrior> <magician> <hero> <name>主教</name> <weapons>长杖、短仗</weapons> <skill>圣光普照</skill> </hero> <hero> <name>火毒法师</name> <weapons>长杖、短仗</weapons> <skill>末日火焰</skill> </hero> <hero> <name>冰雷法师</name> <weapons>长杖、短仗</weapons> <skill>冰咆哮</skill> </hero> </magician> <archer> <hero> <name>神射手</name> <weapons>弓</weapons> <skill>暴风箭雨</skill> </hero> <hero> <name>箭神</name> <weapons>弩</weapons> <skill>终极扫射</skill> </hero> </archer> <ranger> <hero> <name>侠盗</name> <weapons>短剑、短刀</weapons> <skill>暗杀</skill> </hero> <hero> <name>隐士</name> <weapons>拳套</weapons> <skill>四连镖</skill> </hero> <hero> <name>暗影双刀</name> <weapons>短剑、短刀</weapons> <skill>终极斩</skill> </hero> </ranger> </game>
game.xsd文件:
<?xml version="1.0" encoding = "UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn" elementFormDefault="qualified" /> <!--简单元素的定义--> <xs:element name = "name" type = "xs:string" /> <xs:element name = "weapons" type = "xs:string" /> <xs:element name = "skill" type = "xs:string" /> <!--复杂元素的定义--> <xs:element name = "hero"> <xs:complexType> <xs:sequence> <xs:element ref = "name" /> <xs:element ref = "weapons" /> <xs:element ref = "skill" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name = "warrior"> <xs:complexType> <xs:sequence> <xs:element ref = "hero" maxOccurs = "unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name = "magician"> <xs:complexType> <xs:sequence> <xs:element ref = "hero" maxOccurs = "unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name = "archer"> <xs:complexType> <xs:sequence> <xs:element ref = "hero" maxOccurs = "unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name = "ranger"> <xs:complexType> <xs:sequence> <xs:element ref = "hero" maxOccurs = "unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name = "game"> <xs:complexType> <xs:sequence> <xs:element ref = "warrior" /> <xs:element ref = "magician" /> <xs:element ref = "archer" /> <xs:element ref = "ranger" /> </xs:sequence> </xs:complexType> </xs:element>
2016/1/27修订 By野马菌
时间: 2024-12-13 16:55:02