1,xml可以描述数据的结构:
如下面简单的xml文件
<?xml version="1.0" encoding="utf-8"?>
<article>
<title>Simple xml</title>
<date>July 4,2007</date>
<author>
<firstName>John</firstName>
<lastName>Doe</lastName>
</author>
<summary>XML is pretty easy.</summary>
<content>This chapter presents examples that uses xml</content>
</article>
在浏览器中浏览效果:
前面的减号可以单击变为加号,即将其下的子节点信息都隐藏起来了
2,名称空间的应用(用来保证xml元素名称的唯一性)
<?xml version="1.0" encoding="gb2312"?>
<text:directory
xmlns:text = "urn:deitel:textInfo"
xmlns:image= "urn:deitel:imageInfo">
<text:file filename = "book.xml">
<text:description>A book list</text:description>
</text:file>
<image:file filename ="funny.jpg">
<image:description>A funny picture.</image:description>
<image:size width ="200" height= "100" />
</image:file>
</text:directory>
3,默认名称空间,不需每个元素都加前缀
<?xml version="1.0" encoding="utf-8"?>
<!--没有使用前缀的就用默认的前缀,就不需要每个元素都加前缀,xml中用utf-8正确显示中文,用gb-2312显示乱码-->
<directory
xmlns = "urn:deitel:textInfo"
xmlns:image= "urn:deitel:imageInfo">
<file filename = "book.xml">
<description>A book list</description>
</file>
<image:file filename ="funny.jpg">
<image:description>A funny picture.</image:description>
<image:size width ="200" height= "100" />
</image:file>
</directory>
4,使用dtd约束xml文档
<?xml version="1.0" encoding="utf-8"?>
<!--Fig. 12.4: letter.xml -->
<!--Business letter marked up as XML -->
<!DOCTYPE letter SYSTEM "letter.dtd">
<letter>
<contact type ="sender">
<name>Jane doe</name>
<address>box 233</address>
<city>beijing</city>
<state>haidian</state>
<zip>100876</zip>
<phone>555-456</phone>
<flag gender = "F" />
</contact>
<contact type ="receiver">
<name>Jane doe</name>
<address>box 233</address>
<city>beijing</city>
<state>haidian</state>
<zip>100876</zip>
<phone>555-456</phone>
<flag gender = "M" />
</contact>
<salutation>Dear sir:</salutation>
<paragraph>It is our privilege to inform you about new database managed by xml</paragraph>
<paragraph>Please visit our website for more information</paragraph>
<closing>sincerely,</closing>
<signature>Ms. Jane doe</signature>
</letter>
letter.dtd
<!ELEMENT letter (contact+,salutation,paragraph+,closing,signature)>
<!ELEMENT contact (name,address,city,state,zip,phone,flag )>
<!ATTLIST contact type CDATA #IMPLIED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ATTLIST flag gender (M|F) "M">
<!ELEMENT salutation (#PCDATA)>
<!ELEMENT paragraph (#PCDATA)>
<!ELEMENT closing (#PCDATA)>
<!ELEMENT signature (#PCDATA)>元素和约束元素类型的左括号必须有一个空格
5,用模式约束xml文档,xsd比dtd有更多的好处,因为其本身也是xml文档,还可以指定xml元素的类型
<?xml version="1.0" encoding="utf-8"?>
<!--Fig. 12.4: letter.xml -->
<!--Business letter marked up as XML -->
<deitel:books xmlns:deitel = "http://www.deitel.com/booklist" >
<book>
<title>Visual basic 2005</title>
</book>
<book>
<title>java</title>
</book>
<book>
<title>c++</title>
</book>
</deitel:books>
book.xsd
<?xml version="1.0" encoding="utf-8"?>
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
xmlns:deitel ="http://www.deitel.com/booklist"
targetNamespace ="http://www.deitel.com/booklist">
<element name = "books" type = "deitel:BooksType" />
<complexType name ="BooksType">
<sequence>
<element name = "book" type = "deitel:SingleBookType" minOccurs ="1" maxOccurs ="unbounded" />
</sequence>
</complexType>
<complexType name = "SingleBookType">
<sequence>
<element name = "title" type ="string" />
</sequence>
</complexType>
</schema>