XML

用途: 数据传输 配置文件

发明目的是为了 传输和存储数据

优点解析快,缺点占用空间大,现在多用 JSON YAML 替代

类似HTML,XML标签必须自定义

基础 Grammer

属性无序

XML必须有一个根节点

头声明,可选:

1
2
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="utf-8"?>

所有XML都必须是成对标签

大小写敏感

注视和HTML一样 <!-- -->

转义字符也和HTML一样

属性 : 增加解析复杂度

CDATA

CDATA区域不解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
]]>
</script>

命名空间

前缀

1
2
3
4
5
6
7
8
9
10
11
12
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

xmlns 属性

当在 XML 中使用前缀时,一个所谓的用于前缀的命名空间必须被定义

命名空间是在元素的开始标签的 xmlns 属性中定义的。

命名空间声明的语法:xmlns:前缀="URI"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<root>

<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table xmlns:f="http://www.w3cschool.cc/furniture">
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

</root>

命名空间,可以在他们被使用的元素中或者在 XML 根元素中声明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3cschool.cc/furniture">

<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>

<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>

</root>

命名空间 URI 不会被解析器用于查找信息。

默认的命名空间

省去前缀

语法: xmlns="namespaceURI"

1
2
3
4
5
6
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>