标记语言 XML

XML,eXtensible Markup Language,可扩展标记语言。

一、简介

1. 什么是 XML ?

XML,eXtensible Markup Language,可扩展标记语言,被设计用来传输和存储数据。

2. XML 和 HTML 的异同

  • XML 被设计用于传输和存储数据

    HTML 被设计用于显示数据

  • XML 中没有预定义的标签,任何标签都是可自定义的

    HTML 只能使用标准中定义过的标签

  • XML 并不是 HTML 的替代

二、XML 语法

1
2
3
4
<?xml version="1.0" encoding="编码规范"?>
<根标签>
···
</根标签>
  • XML 声明:XML 声明是可选的,如果存在则必须放置到第一行

    1
    <?xml version="1.0" encoding="编码规范"?>

    其中,

    • version="1.0":代表 XML 版本为 1.0

      XML - Wikipedia

      Versions

      There are two current versions of XML:

      XML 1.0

      The first (XML 1.0) was initially defined in 1998. It has undergone minor revisions since then, without being given a new version number, and is currently in its fifth edition, as published on November 26, 2008. It is widely implemented and still recommended for general use.

      XML 1.1

      The second (XML 1.1) was initially published on February 4, 2004, the same day as XML 1.0 Third Edition, and is currently in its second edition, as published on August 16, 2006. It contains features (some contentious) that are intended to make XML easier to use in certain cases. The main changes are to enable the use of line-ending characters used on platforms, and the use of scripts and characters absent from Unicode 3.2. XML 1.1 is not very widely implemented and is recommended for use only by those who need its particular features.

      对于大多数用途,使用 1.0 版本即可

    • encoding="编码规范":代表 XML 文档的编码规范,可以填入 utf-8、gbk 等

  • 根标签:XML 文档中必须有且仅有一个根标签

  • 标签必须被关闭:在 XML 中,省略关闭标签是非法的

  • 区分大小写:XML 标签对大小写敏感

  • 标签应该被正确嵌套

    1
    2
    3
    4
    5
    // 错误
    <b><i>This text is bold and italic</b></i>

    // 正确
    <b><i>This text is bold and italic</i></b>
  • 属性值应该加引号:与 HTML 类似,XML 也可以拥有属性,并且属性必须加引号

    单引号双引号均可以

  • 空格将被保留:与 HTML 不同,XML 中空格将会被保留

三、注意事项

1. 避免使用 XML 属性

使用 XML 属性将会引发某些问题:

  • 属性不能包含多个值
  • 属性不能包含树结构
  • 属性不容易扩展

正确的做法是:将属性转换为元素的子元素

使用属性:

1
2
3
<note date="10/01/2008">
<body>Don't forget me this weekend!</body>
</note>

将属性转换为子元素:

1
2
3
4
<note>
<date>10/01/2008</date>
<body>Don't forget me this weekend!</body>
</note>

更好做法,将属性转换为可扩展的子元素:

1
2
3
4
5
6
7
8
<note>
<date>
<day>10</day>
<month>01</month>
<year>2008</year>
</date>
<body>Don't forget me this weekend!</body>
</note>

参考