Home  >  Article  >  Backend Development  >  Getting Started with XML

Getting Started with XML

黄舟
黄舟Original
2017-02-20 15:04:271337browse

XMLGetting started


# Basic overview


## Extensible Markup Language, a subset of Standard Universal Markup Language, is a markup language used to mark electronic files to make them structural. In electronic computers, tags refer to information symbols that computers can understand. Through such tags, computers can process various information such as articles, etc. It can be used to mark data and define data types. It is a source language that allows users to define their own markup language. It is ideally suited for World Wide Web transport, providing a unified approach to describing and exchanging structured data independent of applications or vendors. It is a cross-platform, content-dependent technology in the

Internet

environment, and it is also an effective tool for processing distributed structured information today. As early as 1998, W3C released the XML1.0 specification, Use it to simplify the transmission of document information on the Internet.

The historical origin of XML

In 1969

,

GML(Generalized Markup Language Generalized Markup Language)---->In 1985,SGML(Standard Generalized Markup LanguageStandard Generalized Markup Language)---> ;1993年,HTML(Hypertext Markup LanguageHypertext Markup Language)--->1998 Year, XML(Extensible Markup LanguageExtensible Markup Language)

What is extensible markup language?

1

, Extensible Markup Language is a markup language that is very similar to Hypertext Markup Language.

2

, It is designed to transmit data, not display data.

3

, its label is not predefined. You need to define the labels yourself.

4

, It is designed to be self-descriptive.

5

, it is the recommended standard of

W3C.

What is the difference between Extensible Markup Language and Hypertext Markup Language?

1

, it is not a replacement for Hypertext Markup Language.

2

, it is a supplement to Hypertext Markup Language.

3

, It is designed for different purposes from Hypertext Markup Language:

4

, It is designed to transmit and store data , whose focus is the content of the data.

5

, Hypertext Markup Language is designed to display data, with the focus being on the appearance of the data.

6

, Hypertext Markup Language is designed to display information, while it is designed to transmit information.

7

. The best description of it is: it is an information transmission tool independent of software and hardware.

Why do you need

XML

? 1

, solves the problem of non-standard data transmission.

2

, can describe things in a tree structure very well.

3

, can be used as a configuration file.

PS

: Nowadays, many languages ​​and technologies are using XML as data transmission standard, so a deep understanding of XML is equivalent to mastering a general data transmission protocol. Reference document:

http://www.php.cn/


Case:

<?xml version="1.0" encoding="UTF-8"?>
<class>
	<stu id="a001">
		<name>张三</name>
		<sex>男</sex>
		<age>20</age>
	</stu> 
	<stu id="a002">
		<name>李四</name>
		<sex>女</sex>
		<age>18</age>
	</stu>
</class>

XML基本语法

一个XML文件可分为如下几部分内容:

文档声明 、元素、属性、注释  、CDATA区 ,特殊字符 、处理指令(processing instruction) 

基本语法:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 上面是文档声明 - ->
<?xml-stylesheet type="text/css" href=”XML2.css”?>
<!-- 上面是处理指令 - ->
<根元素>
    <!-- 注释 - ->
    <![CDATA[     CDATA区,可以是任意字符    ]]>
	<元素 属性=”属性值”>
		<元素>元素内容</元素>
		<空元素/>>
	</元素> 
</class>


文档声明

<?xml version="1.0" encoding=“编码方式"  standalone="yes|no"?>

XML声明放在XML文档的第一行

XML声明由以下几个部分组成:

version - -文档符合XML1.0规范

encoding - -文档字符编码,比如”utf-8”

standalone - -文档定义是否独立使用

standalone="yes

standalone=no”   默认

 

PS:虽说现在XML出了2.0版了,但是现在大多还是用1.0版。

 

元素

基本语法:

<元素>元素内容</元素>
<元素/>


注意事项:

1每个XML文档必须有且只有一个根元素。

2根元素是一个完全包括文档中其他所有元素的元素。

3根元素的起始标记要放在所有其他元素的起始标记之前。

4根元素的结束标记要放在所有其他元素的结束标记之后。

5XML元素指XML文件中出现的标签,一个标签分为开始标签和结束标签,一个标签有如下几种书写形式,例如:

    包含标签体:3499910bf9dac5ae3c52d5ede73834851235db79b134e9f6b82c0b36e0489ee08ed

    不含标签体的:3499910bf9dac5ae3c52d5ede73834855db79b134e9f6b82c0b36e0489ee08ed, 简写为:7d01d0a03a49fceb57d12197d8d196cb

6一个标签中也可以嵌套若干子标签。但所有标签必须合理的嵌套,绝对不允许交叉嵌套 ,例如:

        3499910bf9dac5ae3c52d5ede7383485hello a4b561c25d9afb9ac8dc4d70affff419world5db79b134e9f6b82c0b36e0489ee08ed0d36329ec37a2cc24d42c7229b69747a 

7对于XML标签中出现的所有空格和换行,XML解析程序都会当作标签内容进行处理。

例如:

        3499910bf9dac5ae3c52d5ede73834851235db79b134e9f6b82c0b36e0489ee08ed3499910bf9dac5ae3c52d5ede7383485   123   5db79b134e9f6b82c0b36e0489ee08ed意义是完全不同的。

8一个XML元素可以包含字母、数字以及其它一些可见字符,但必须遵守下面的一些规范:

    1---区分大小写,例如,64e5601d0a941f4972a2954192bdae18e388a4556c0f65e1904146cc1a846bee是两个不同的标记。

    2---不能以数字或"_" (下划线)开头。

    3---不能包含空格。

    4---名称中间不能包含冒号(:)。

9、元素、标签、节点意义都是一样的。


属性

基本语法

<元素 属性1=”属性值” 属性2=”属性值”>元素内容</元素>


注意事项:

1属性值用双引号(")或单引号(')分隔(如果属性值中有',用"分隔;有",用'分隔)

2一个元素可以有多个属性,它的基本格式为:

3属性名称在同一个元素标记中只能出现一次

4属性值不能包括83992e84119b8649428fe2de73af99ef, &之类的特殊字符,否则需要使用转义字符。



注释


    这个和HTML一样,都是c2a54aadee89294d3edc03e4891272d5,同样也不能注释嵌套,不能放在元素名中间。

 

CDATA

    有些内容可能不想让解析引擎解析执行,而是当作原始内容处理,用于把整段文本解释为纯字符数据而不是标记的情况。包含大量509d37b71e5ee94f7a50f124edbc8c3f&或者"字符。CDATA区中的所有字符都会被当作元素字符数据的常量部分,而不是XML标记。

 

PSCDATA区可以用于传递特殊字符,传递文件数据。可以通过将图片这类二进制文件以byte[]的形式放入CDATA区中,需要使用时在以byte[]的方式读出。

 

处理指令

    处理指令,简称PI (Processing Instruction)。处理指令用来指挥解析引擎如何解析XML文档内容。

    处理指令必须以“0f7ea912b4bec2649ab2166088880186”作为结尾,XML声明语句就是最常见的一种处理指令。 例如,在XML文档中可以使用xml-stylesheet指令,通知XML解析引擎,应用css文件显示xml文档内容。


案例:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="XML2.css" type="text/css"?>
<class>
	<student>
		<name>张三</name>
		<sex>男</sex>
		<age>20</age>
	</student>
	<student>
		<name>李四</name>
		<sex>女</sex>
		<age>18</age>
	</student>
</class>


XML2.css

name {
	font-size: 20px;
	font-weight: bold;
	color: red;
}

sex{
	font-size: 30px;
	font-weight: bolder;
	color: blue;
}

age{
	font-size: 25px;
	font-weight: bolder;
	color: blue;
}


小结

1XML必须有且仅有一个根元素

2、元素大小写敏感

3、元素不能以数字,下划线开头

4属性值用引号

5、属性值如果有特殊字符要用实体表示

6、同一元素的属性必须唯一,属性值可以不唯一

7、非空元素标记必须成对

8空标记要写关闭符号

9元素必须正确嵌套

10、元素中可以包含字母、数字或者其它字符(支持中文)

11、元素中不能含空格

12、元素中不能含冒号(注:冒号留给命名空间使用

 以上就是XML入门的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn