xml基础

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>

在浏览器中浏览效果:

[img]http://dl.iteye.com/upload/attachment/355087/fbfad7dc-e3b2-301f-9233-65c1bb6fdddf.jpg[/img]

前面的减号可以单击变为加号,即将其下的子节点信息都隐藏起来了


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>

[b]letter.dtd[/b]
[b]<!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)>[/b]元素和约束元素类型的左括号必须有一个空格
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>

[b]book.xsd[/b]
<?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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值