XMl学习记录
xml 全称为extensible Makeup Language,被设计用作传输和储存。
1.xml设计的初衷是用来传输数据, 而不是显示数据
2.xml也有标签,但标签没有预定义, 可以自行定义
3.xml被设计为具有自我描述性
对xml最好的描述:独立于软件和硬件的信息传输工具
一个xml文档实例
<?xml version="1.0" encoding="UTF-8">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don`t forget me this weekend!</body>
</note>
第一行是xml声明,它定义了xml的版本(1.0)和所使用的编码(UTF-8)。
note是描述文档的根元素
接下来四行是描述根的四个元素(to, from, heading, body)
xml文档是一个树结构
xml 文档必须包含根元素,该元素是其他元素的父元素
xml文档形成了一颗文档树, 从树的根开始, 一直扩展到树的最底端。
xml语法
xml文档必须要有根元素
xml声明是可选部分, 如果存在需要放在文档的第一行, xml声明如下:
<?xml version="1.0" encoding="UTF-8"?>
xml的每个元素都必须拥有关闭标签(文档声明不是xml文档本省的一部分, 所有没有关闭标签)
xml对大小写敏感
xml属性值必须加引号
实体引用
在xml中有一些字符比较特殊, 如把"<"放在文档内容中会引发错误,它会被认为新的起始标签, 如下:
<body>if salary < 1000 then</body>
为了避免这些错误, 可以采用实体引用, 如下:
<body>if salary < 1000 then</body>
在xml中, 有五个预定义的实体引用:
< | < | less than |
---|---|---|
> | > | greate than |
& | & | ampersand |
' | ’ | apostrophe |
" | " | quotation mark |
注释: 在xml中, 只有字符"<“和”&"是非法的, > 是合法的,但是采用实体引用代替这些符号是一个好习惯。
xml 会保留空格
HTML会把连续的空格裁剪成一个空格
xml中会保留原始的空格
xml以LF储存新行
回车符CR, 换行符LF
xml元素
xml元素是指从起始标签到结束标签所包含的内容, 其包括:
- 其它元素
- 文本内容
- 属性及属性值
- 或混合以上所有
<bookstore>
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowing</author>
<year>2005</year>
<price>29.9</price>
</book>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
在上面的实例中bookstore和book元素里面都包含另外的元素
xml元素命名
xml 元素标签不能包含空格
不能以标点符号和数字开头
xml元素属性
xml应尽量避免使用属性,如果属性看起来像数据, 就尽量用元素来表示
<note date="10/01/2008">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note>
<date>
<day>10</day>
<month>01</month>
<year>2008</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>