xml学习

[size=medium]
本章节目的:
1.怎么用start-tags和end-tags 来create XML elements?
2.怎么样进一步描述elements with attributes
3.怎么申明你的document 是xml
4.怎么发送instruction 给处理xml document的application
5.xml 不允许出现的charactors——在你的document 中怎么样使用他们
?
Tags and Text and Elements, Oh my! :举例来说,<first>是start-tag, </first>是end-tag, 而<first/>属于self-closing tag, 在start-tag 和end-tag 之间的被称为element content, 也可以说是Parsed Charactor DATA (PCDATA)或是text node , text content, PCDATA 是来自于SGML的说法。

Rules for elements:
1.每一个start-tag 都有匹配的end-tag,或是一个self-closing tag
2.tags 不能overlap, 必须正确nested
3.一个xml 只能有一个root elements,这个root element是针对xml ducument而言的 ,即是在层级结构中处于xml 顶部的。这个rule 就指明了不能出现这种情况:
<name>John</name>
<name>jane</name>
这样就是出现了两个root elements, 我们可以这么修改,
<names>
<name>John</name>
<name>jane</name>
</names>
4.Element names 必须遵守xml 的命名规则。(xml名字的第一个字母后是允许有点号(periods)的,不允许的情况就不细说了)。
5.XML 是case sensitiv 的
6.xml将保留whitespace在你的PCDATA中。这一点和html 不同,比如说:
html中,<p> this is paragraph. It has a whole bunch of space</p>
将显示为this is paragraph. It has a whole bunch of space,
而xml中它将显示为:this is paragraph. It has a whole bunch of space.
End of Line Whitespace: xml 会自动处理,因此programmers 不用去处理(end-of-line)换行的逻辑。


Attribute: xml document 除了elements 和tags 之外也能有attributes, attributes 是分给elements 的name/value pairs,例子如下:
<name nickname=" Shiny J>
<first>John</first>
<last>Doe</last>
</name>
Attributes 必须有值,而值又必须放在一对双引号或是单引号中。缺少引号或是引号不配对或是缺少值都是不对的。一个elements 不能有多个相同的attribute , 比如说<bad att="1" att="2"></bad>就是错误的,在IE XML parse中出现错误提示:Duplicate attribute.
Attribute在elements中的顺序是不重要的,不相关的,无意义的,任何顺序都可以,因此如果information 有顺序的要求, 你应该将它放在elements之中而不是attributes 里。
attribute能够加入额外信息比如说list 的number 之类的。http://hi.baidu.com/ousysrobin

attribute 能够分开不同类型的信息,基本上attribute 能做的elements 都行, attribute不用考虑cross-tag 的问题, 不过如果用attribute , 就会丢掉去多xml 的有点,比如说readability和descriptive tag names. elements 比attribute更加complex.

Comments: comments 提供了注释方法, 它并不真正属于该xml 文档,只是为了方便理解而已。注释是通过<!--和-->来完成的,注意comments不能在tag 中。comments 可能会递给也可能不会递给application,取决于我们所选的parser.
<name nickname=" Shiny J>
<first>John</first>
<middle></middle>
<last>Doe</last>
</name>
像middle中间并没有包括任何name , 可以这么写<middle/>,即是说使用self-closing tag

XML Declaration: 能够去识别文档类型是非常好的,windows提供了.xml的扩展名来指示是一个xml文件,可是其他的操作系统就没有提到这点了, xml 提供了xml declaration去指示一个文档是xml 文档,顺带提供了一些其他的信息,一个parser通常都能够识别xml 文档即使没有xml declaration,但是最好还是注明xml declaration比较好,xml 声明如下:
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
<name nickname=" Shiny J>
<first>John</first>
<middle></middle>
<last>Doe</last>
</name>

xml声明是由<?xml开始,由?>结束,如果要做xml declaration , version是必须写明的, 但是encoding和 standalone 是可选的,即是说<?xml version='1.0'?>也是正确的。xml的申明必须在document的开始, 也就是说document开始的第一个字符就是<,不能够有空行或空格。同时必须注意如果三者都写了,那么version , encoding , standalone 三者的顺序不能变更。

standalone 的值yes只得是文档可以单独存在,不依赖与其他文件,而no 指的是文件may 依赖于外部的DTD 。

Processing Instructions:虽然它并不common,但是有时候你需要去嵌入一段application-specific instructions到你的information中去影响how it will be processed, 这种机制被称之为PIs或是processing instructions ,一个例子:(下面的这个例子是书上的,fire 也是书上这么写的)http://hi.baidu.com/ousysrobin
<?xml version='1.0'?>
<name nickname="Shiny John">
<!--John lost his middle name in a fire-->
<middle/>
<?nameprocessor SELECT * FROM blah?>
<last>Doe</last>
</name>
其中红色字体代表的部分就是PIs,它的规则是以<?开头然后followed by 将接收PIs的(PITarget)的application的名称,上面的例子的PITarget是nameprocessor, 而PI的actual text 是SELECT * FROM blah
需要注意xml declaration 并不是PI, 大部分的application 并不会接收到xml declaration 的信息,(XML Declaration只是为了parser),可却一定接收PI, XML Declaration只能在document 开头,而PI却能在任何地方。PI对xml 本身并没有什么作用, 只是交给application 去做一些meaningful 的事情。


Illegal PCDATA characters :xml中有一些reserved characters, 比如说<和&,下面是个有错误的例子:<!--This is not well-formed XML!-->
<comparison>6 is < 7 & 7 > 6</comparison>
应该改为:<comparison>6 is < 7 & 7 > 6 </comparison>
几个常见的替换encaping characters:(左边的也被称为entity reference)
&——the & character
<——the < charactor
>——the > charactor
&apos;——the ' charactor
"——the " charactor
其他的能够encaped by using charactor reference ,就是#nnn; ,nnn是16进制的,参照unicode number of the charactor you want to insert.比如说copyright符号(©)就被表示为©或是©.

CDATA: 如果你的文档中有很多的<和&, 转义符是不适合,此时就得需要用到CDATA了,CDATA是来源于SGML的,它代表Charactor Data,通过CDATA, xml parser 能不去parse 这个text ,直到CDATA section 的结束。例子:(这里的文件命是cdatahere.xml)
<script language='JavaScript'><![CDATA[
function myFunc()
{
if(0 < 1 && 1 < 2)
alert("Hello");
}
]]></script>
显示在屏幕上的就是:
<script language='JavaScript'>
<![CDATA[
function myFunc()
{
if(0 < 1 && 1 < 2)
alert("Hello");
}
]]>
</script>
注意在<![CDATA[和]]>之间的xml parser 都不会被parse的,将按原样显示出来。

Errors in XML: 里面的错误能够有两种类型, 一种是xml parser能让它自动回复,另一种是比较严重的, xml parser 会中断解析, 这两种都是在一定程度上违反了xml well-formed rule 的, 注意在xml 发生错误是不同的parser 可能提示不同的错误,即使是第一种情况,有时候parser 也会解析的和你原有的意思不同。[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值