what is XML?
XML (可scale标记language, eXtensible Markup Language) is a用于store and 传输data 标记language, 由W3C (万维网联盟) 于1998年release. and HTMLclass似, XMLusingtag来describesdata, 但 and HTML不同 is , XML tag不 is 预定义 , 而 is user可以根据需要自定义 .
XML design目标 is data store and 传输, 而不 is data 显示. 它providing了一种structure化 方式来表示data, 使得data可以 in 不同 system and application之间easily交换.
XML history背景
XML 发展可以追溯 to SGML (标准common标记language) , 这 is a用于定义documentationstructure complex language. XML is SGML 一个简化version, 旨 in 保留SGML 强 big functions, 同时降 low 其 complex 性 and Learning曲线.
- 1996年: W3C开始XML Development工作
- 1998年: W3CreleaseXML 1.0推荐标准
- 2000年: W3CreleaseXML Schema 1.0
- 2004年: W3CreleaseXML 1.1, 但XML 1.0仍然 is 最广泛using version
- 2006年: W3CreleaseXML Schema 1.1
XML application场景
XML in 各种领域都 has 广泛 application, including:
- data交换: 不同system and application之间 data传输
- configurationfile: 许 many 软件 and frameworkusingXMLserving asconfigurationfile格式
- Webservice: SOAP ( simple object访问protocol) usingXML来formatmessage
- in 容management: 用于store and management网站 in 容
- 电子data交换 (EDI) : 用于商业documentation 电子交换
- RSS and Atom: 用于release and subscribe网站 in 容
XMLBasics语法
XML 语法规则相 for simple , 但必须严格遵守. 以 under is XML basic语法规则:
XMLdocumentationstructure
一个完整 XMLdocumentation通常package含以 under 部分:
- XML声明: 可选 , 用于指定XMLversion and 编码
- documentationclass型声明 (DTD) : 可选 , 用于定义XMLdocumentation structure
- 根元素: 必须 , XMLdocumentation 顶层元素
- 子元素: 根元素 in 元素
- property: providing元素 附加information
- comment: 用于添加说明, 不会被解析器processing
- 实体引用: 用于表示特殊字符
XML声明
XML声明 is 可选 , 但such as果package含, 必须放 in documentation 第一行. 它用于指定XMLversion and 编码:
<?xml version="1.0" encoding="UTF-8"?>
tag规则
XMLtag is 区分 big small 写 , 必须成 for 出现, 并且正确嵌套:
- 每个开始tag必须 has 一个 for 应 结束tag (例such as:
<book>and</book>) - 空tag可以using自闭合语法 (例such as:
<br />) - tag必须正确嵌套, 不能交叉 (例such as:
<div><p> in 容</div></p>is error ) - 标signature称可以package含字母, number, 连字符, under 划线 and 点, 但不能以number or 标点符号开头
- 标signature称不能package含空格
元素example
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book>
<title>XMLBasicstutorial</title>
<author>张三</author>
<year>2025</year>
<price>99.00</price>
</book>
<book>
<title>Pythonprogramming入门</title>
<author>李四</author>
<year>2024</year>
<price>89.00</price>
</book>
</library>
property
property用于providing元素 附加information, 它们 in 开始tagin定义:
- property值必须用引号 (单引号 or 双引号) 括起来
- 一个元素可以 has many 个property, 但property名不能重复
- property名 命名规则 and 元素名相同
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book id="001" category="计算机">
<title>XMLBasicstutorial</title>
<author>张三</author>
<year>2025</year>
<price>99.00</price>
</book>
<book id="002" category="计算机">
<title>Pythonprogramming入门</title>
<author>李四</author>
<year>2024</year>
<price>89.00</price>
</book>
</library>
comment
XMLcomment用于添加说明, 不会被XML解析器processing:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 这 is a XMLcomment -->
<library>
<book id="001">
<title>XMLBasicstutorial</title>
<author>张三</author>
<!-- 这 is 另一个comment -->
<year>2025</year>
<price>99.00</price>
</book>
</library>
实体引用
in XMLin, 某些字符具 has 特殊含义, 不能直接using. 需要using实体引用来表示这些字符:
| 特殊字符 | 实体引用 | describes |
|---|---|---|
| < | < | small 于号 |
| > | > | big 于号 |
| & | & | and 号 |
| " | " | 双引号 |
| ' | ' | 单引号 |
<?xml version="1.0" encoding="UTF-8"?>
<message>
<content>5 < 10</content>
<content>"Hello World"</content>
<content>This & That</content>
</message>
实践case: creation一个 simple XMLdocumentation
casedescribes
creation一个XMLdocumentation, 用于store学生information, including学生 姓名, 年龄, 性别 and 成绩.
implementation步骤
- 添加XML声明
- creation根元素
<students> - in 根元素 in 添加 many 个
<student>子元素 - for 每个学生添加
<name>,<age>,<gender>and<score>子元素 - for 学生元素添加唯一
idproperty
最终code
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="1001">
<name>张三</name>
<age>20</age>
<gender>男</gender>
<score>85</score>
</student>
<student id="1002">
<name>李四</name>
<age>21</age>
<gender>女</gender>
<score>92</score>
</student>
<student id="1003">
<name>王五</name>
<age>19</age>
<gender>男</gender>
<score>78</score>
</student>
</students>
互动练习
练习1: 识别XML语法error
<?xml version="1.0" encoding="UTF-8"?>
<books>
<Book>
<title>XMLBasicstutorial</title>
<author>张三</author>
</book>
<book>
<title>Pythonprogramming</title>
<author>李四</author>
<year>2024
</book>
<book id=123>
<title>Java入门</title>
<author>王五</author>
</book>
</books>
- tag big small 写不一致:
<Book>and</book> - 缺 few 结束tag:
<year>没 has for 应</year> - property值没 has 用引号括起来:
id=123应该 isid="123"
练习2: creationXMLdocumentation
<?xml version="1.0" encoding="UTF-8"?>
<courses>
<course id="C001">
<name>XMLBasics</name>
<teacher>赵老师</teacher>
<hours>32</hours>
<credits>2</credits>
</course>
<course id="C002">
<name>Pythonprogramming</name>
<teacher>钱老师</teacher>
<hours>48</hours>
<credits>3</credits>
</course>
<course id="C003">
<name>datalibrary原理</name>
<teacher>孙老师</teacher>
<hours>40</hours>
<credits>2.5</credits>
</course>
</courses>