XML—XML introduction and basic syntax

黄舟
Release: 2017-02-24 14:53:43
Original
1292 people have browsed it

1.XML History

gml(1969)->sgml(1985)->html(1993)- >xml(1998)

  • ##1969 gml (General Markup Language), the main purpose is to communicate data specifications between different machines

  • 1985 sgml (Standard Generalized Markup Language)

  • 1993 html (Hypertext Markup Language, www network)

html The language itself has some flaws

(1) Tags cannot be customized
(2) HTML itself lacks meaning
(3) HTML is not truly internationalized

There is an intermediate transition language, xhtml:

html->xhtml->xml

  • 1998 xml extensiable markup language Extensible Markup Language

##2. Why is XML

1. Requirement 1

Data communication between two programs?

2. Requirement 2
Make a configuration file for a server. When the server program starts, read the port number it should listen to, as well as the user name and password for connecting to the database?

In XML language, it allows users to customize tags. A tag is used to describe a piece of data; a tag can be divided into a start tag and an end tag. Between the start tag and the end tag, other tags can be used to describe other data to achieve the description of data relationships.

3. Common applications of XML

1.The emergence of XML solves the problem of data transmission between programs:

For example, data transmission between QQ uses XML format to transmit data, with good readability and maintainability


2.XML can be used as configuration files

It can be said that XML files can be used as configuration files, such as server.xml of our Tomcat server. , web.xml. Another example is the structs-config.xml file in our structs, hibernate's hibernate.cfg.xml, etc.


3.XML can be used as a small database

XML files can be used as small databases, which is also a good choice. Our program may use some data that often requires manual configuration. If it is read in the database If it is not suitable (because it will increase the work of maintaining the database), you can consider using XML directly to make a small database. Reading files directly in this way is obviously faster than reading the database. For example, XML files are used to save user chat records in msn.


Getting Started Case: Use XML to record a class information.

<?xml version="1.0" encoding="gb2312"?><class>
    <stu id="001">
        <name>杨过</name> 
        <sex>男</sex>
        <age>20</age>
    </stu>  
    <stu id="002">
        <name>小龙女</name>    
        <sex>女</sex>
        <age>21</age>
    </stu></class>
Copy after login
Copy after login

We can open it with a browser:

XML—XML introduction and basic syntaxSo can our XML be displayed on the web page like html? It is also possible, it can also be modified with css, but we don't use it, we only need to use XML to store data.

In this example, if we change the encoding of the first line to utf-8, and then open it with a browser, an error will be reported. Why is this?

Because the default encoding of xml files is ANSI, which is the encoding developed by the American National Standards Institute. It has formulated different standards according to different countries and regions. In China, it is GB2312, so we use GB2312 There will be no errors in encoding, but an error will be reported when using UTF-8.

The solution is to change the XML file to UTF-8 encoding mode.

4.XML syntax

An XML file is divided into the following parts:

1.Document declaration

2.Element
3.Attributes
4.Comments
5.CDATA area, special characters
6.Processing instruction

4.1.XML syntax-document declaration

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
Copy after login
Copy after login
The XML declaration is placed in the first line of the XML document

The XML declaration consists of the following parts:


version – the document conforms to the XML1.0 specification, we learn 1.0
encoding – document character encoding, such as “GB2312” or “UTF-8”

standalone – whether the document definition is used independently
standalone="no" is the default value. Yes means it is used independently, while no means it is not used independently

4.2. XML syntax - element (or tag, node)

( 1) Each XML document must have one and only one
root element

    The root element is an element that completely includes all other elements in the document
  • The start tag of the root element must be placed before the start tags of all other elements
  • The end tag of the following element must be placed before the start tags of all other elements After the end tag
(2) XML elements refer to tags that appear in XML files. A tag is divided into a start tag and an end tag. A tag can be written in the following ways For example,

    contains tag body:
  • <a>www.sohu.com</a>
    Copy after login
    Copy after login
    without tag body:
  • <a></a>,简写为:<a/>
    Copy after login
    Copy after login
(3) Several sub-tags can also be nested in one tag. However, all tags must be reasonably nested, and cross-nesting is absolutely not allowed. For example,

<a>welcome to <b> www.sohu.com </a></b>
Copy after login
Copy after login

will definitely result in an error.

(4)对于XML标签中出现的所有空格和换行,XML解析程序都会当做标签内容进行处理。例如下面两段内容的意义是不一样的。

<stu>xiaoming</stu>
Copy after login
Copy after login

和如下:

<stu>
    xiaoming</stu>
Copy after login
Copy after login

(5)由于在XML中,空格和换行都作为原始内容被处理,所以,在编写XML文件时,要特别注意。

(6)命名规范:一个XML元素可以包含字母、数字以及其它一些可见字符,但必须遵守以下规范:

  • 区分大小写,例如,元素P和元素p是两个不同的元素

  • 不能以数字或下划线”_”开头

  • 元素内不能包含空格

  • 名称中间不能包含冒号(:)

  • 可以使用中文,但一般不这么用

4.3.XML语法-属性

<student id="100">
    <name>Tom</name></student>
Copy after login
Copy after login

(1)属性值用双引号(”)或单引号(’)分隔,如果属性值中有单引号,则用双引号分隔;如果有双引号,则用单引号分隔。那么如果属性值中既有单引号还有双引号怎么办?这种要使用实体(转义字符,类似于html中的空格符),XML有5个预定义的实体字符,如下:

XML—XML introduction and basic syntax

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

<元素名 属性名1="属性值1" 属性名2="属性值2">
Copy after login
Copy after login

(3)特定的属性名称在同一个元素标记中只能出现一次
(4)属性值不能包括<,>,&,如果一定要包含,也要使用实体

4.4.XML语法-注释

XML的注释类似于HTML中的注释:

<!--这是一个注释-->
Copy after login

(1)注释内容不要出现--
(2)不要把注释放在标记中间;
(3)注释不能嵌套
(4)可以在除标记以外的任何地方放注释

4.5.XML语法-CDATA节

假如有这么一个需求,需要通过XML文件传递一幅图片,怎么做呢?其实我们看到的电脑上的所有文件,本质上都是字符串,不过它们都是特殊的二进制字符串。我们可以通过XML文件将一幅图片的二进制字符串传递过去,然后再解析成一幅图片。那么这个字符串就会包含大量的<,>,&或者“等一些特殊的不合法的字符。这时候解析引擎是会报错的。

所以,有些内容可能不想让解析引擎解析执行,而是当做原始内容处理,用于把整段文本解释为纯字符数据而不是标记。这就要用到CDATA节。

语法如下:

<![CDATA[
    ......
]]>
Copy after login
Copy after login

CDATA节中可以输入任意字符(除]]>外),但是不能嵌套!

如下例,这种情况它不会报错,而如果不包含在CDATA节中,就会报错:

<stu id="001">
    <name>杨过</name> 
    <sex>男</sex>
    <age>20</age>
    <intro><![CDATA[ad<<&$^#*k]]></intro></stu>
Copy after login
Copy after login

4.6.XML语法-处理指令

处理指令,简称PI(processing instruction)。处理指令用来指示解析引擎如何解析XML文件,看下面一个例子:

比如我们也可以使用css样式表来修饰XML文件,编写my.css如下:

name{    
font-size:80px;    
font-weight:bold;    
color:red;
}
sex{    
font-size:60px;    
font-weight:bold;    
color:blue;
}
sex{    
font-size:40px;    
font-weight:bold;    
color:green;
}
Copy after login
Copy after login

我们在xml文件中使用处理指令引入这个css文件,如下:

<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet href="my.css" type="text/css"?><class>
    <stu id="001">
        <name>杨过</name> 
        <sex>男</sex>
        <age>20</age>
    </stu>  
    <stu id="002">
        <name>小龙女</name>    
        <sex>女</sex>
        <age>21</age>
    </stu></class>
Copy after login

这时候我们再用浏览器打开这个xml文件,会发现浏览器解析出一个带样式的视图,而不再是单纯的目录树了:

XML—XML introduction and basic syntax

但是XML的处理指令不要求掌握,因为用到的很少。

5.格式正规的XML文档-小结

语法规范

1.XML声明语句
2.必须有一个根元素
3.标记大小写敏感
4.属性值用引号
5.标记成对
6.空标记关闭
7.元素正确嵌套

1.XML历史

gml(1969)->sgml(1985)->html(1993)->xml(1998)

  • 1969 gml(通用标记语言),主要目的是要在不同的机器之间进行通信的数据规范

  • 1985 sgml(标准通用标记语言)

  • 1993 html(超文本标记语言,www网)

html语言本身是有一些缺陷的
(1)不能自定义标签
(2)html本身缺少含义
(3)html没有真正的国际化

有一个中间过渡语言,xhtml:
html->xhtml->xml

  • 1998 xml extensiable markup language 可扩展标记语言

2. Why XML is needed

1. Requirement 1
Data communication between two programs?
2. Requirement 2
Make a configuration file for a server. When the server program starts, read the port number it should listen to, as well as the user name and password for connecting to the database?

In XML language, it allows users to customize tags. A tag is used to describe a piece of data; a tag can be divided into a start tag and an end tag. Between the start tag and the end tag, other tags can be used to describe other data to achieve the description of data relationships.

3. Common applications of XML

1.The emergence of XML solves the problem of data transmission between programs:
For example, data transmission between QQ uses XML format to transmit data, with good readability and maintainability

2.XML can be used as configuration files
It can be said that XML files can be used as configuration files, such as server.xml of our Tomcat server. , web.xml. Another example is the structs-config.xml file in our structs, hibernate's hibernate.cfg.xml, etc.

3.XML can be used as a small database
XML files can be used as small databases, which is also a good choice. Our program may use some data that often requires manual configuration. If it is read in the database If it is not suitable (because it will increase the work of maintaining the database), you can consider using XML directly to make a small database. Reading files directly in this way is obviously faster than reading the database. For example, XML files are used to save user chat records in msn.

Getting Started Case: Use XML to record a class information.

<?xml version="1.0" encoding="gb2312"?><class>
    <stu id="001">
        <name>杨过</name> 
        <sex>男</sex>
        <age>20</age>
    </stu>  
    <stu id="002">
        <name>小龙女</name>    
        <sex>女</sex>
        <age>21</age>
    </stu></class>
Copy after login
Copy after login

We can open it with a browser:

XML—XML introduction and basic syntax

So can our XML be displayed on the web page like html? It is also possible, it can also be modified with css, but we don't use it, we only need to use XML to store data.

In this example, if we change the encoding of the first line to utf-8, and then open it with a browser, an error will be reported. Why is this?

Because the default encoding of xml files is ANSI, which is the encoding developed by the American National Standards Institute. It has formulated different standards according to different countries and regions. In China, it is GB2312, so we use GB2312 There will be no errors in encoding, but an error will be reported when using UTF-8.

The solution is to change the XML file to UTF-8 encoding mode.

4.XML syntax

An XML file is divided into the following parts:
1.Document declaration
2.Element
3.Attributes
4.Comments
5.CDATA area, special characters
6.Processing instruction

4.1.XML syntax-document declaration

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
Copy after login
Copy after login

The XML declaration is placed in the first line of the XML document
The XML declaration consists of the following parts:

version – the document conforms to the XML1.0 specification, we learn 1.0
encoding – document character encoding, such as “GB2312” or “UTF-8”
standalone – whether the document definition is used independently
standalone="no" is the default value. Yes means it is used independently, while no means it is not used independently

4.2. XML syntax - element (or tag, node)

( 1) Each XML document must have one and only one root element

  • The root element is an element that completely includes all other elements in the document

  • The start tag of the root element must be placed before the start tags of all other elements

  • The end tag of the following element must be placed before the start tags of all other elements After the end tag

(2) XML elements refer to tags that appear in XML files. A tag is divided into a start tag and an end tag. A tag can be written in the following ways For example,

  • contains tag body:

<a>www.sohu.com</a>
Copy after login
Copy after login
  • without tag body:

<a></a>,简写为:<a/>
Copy after login
Copy after login

(3) Several sub-tags can also be nested in one tag. However, all tags must be reasonably nested, and cross-nesting is absolutely not allowed. For example,

<a>welcome to <b> www.sohu.com </a></b>
Copy after login
Copy after login

will definitely result in an error.

(4) For all spaces and newlines that appear in XML tags, the XML parser will treat them as tag content. For example, the meanings of the following two paragraphs are different.

<stu>xiaoming</stu>
Copy after login
Copy after login

and as follows:

<stu>
    xiaoming</stu>
Copy after login
Copy after login

(5) Since in XML, spaces and newlines are processed as original content, so when writing XML files, you must pay attention.

(6) Naming convention: An XML element can contain letters, numbers, and other visible characters, but must comply with the following conventions:

  • is case sensitive , for example, element P and element p are two different elements

  • cannot start with a number or an underscore "_"

  • cannot be within an element Contains spaces

  • The name cannot contain a colon (:)

  • Chinese can be used, but generally not used like this

4.3.XML语法-属性

<student id="100">
    <name>Tom</name></student>
Copy after login
Copy after login

(1)属性值用双引号(”)或单引号(’)分隔,如果属性值中有单引号,则用双引号分隔;如果有双引号,则用单引号分隔。那么如果属性值中既有单引号还有双引号怎么办?这种要使用实体(转义字符,类似于html中的空格符),XML有5个预定义的实体字符,如下:

XML—XML introduction and basic syntax

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

<元素名 属性名1="属性值1" 属性名2="属性值2">
Copy after login
Copy after login

(3)特定的属性名称在同一个元素标记中只能出现一次
(4)属性值不能包括<,>,&,如果一定要包含,也要使用实体

4.4.XML语法-注释

XML的注释类似于HTML中的注释:

<!--这是一个注释-->
Copy after login

(1)注释内容不要出现--
(2)不要把注释放在标记中间;
(3)注释不能嵌套
(4)可以在除标记以外的任何地方放注释

4.5.XML语法-CDATA节

假如有这么一个需求,需要通过XML文件传递一幅图片,怎么做呢?其实我们看到的电脑上的所有文件,本质上都是字符串,不过它们都是特殊的二进制字符串。我们可以通过XML文件将一幅图片的二进制字符串传递过去,然后再解析成一幅图片。那么这个字符串就会包含大量的<,>,&或者“等一些特殊的不合法的字符。这时候解析引擎是会报错的。

所以,有些内容可能不想让解析引擎解析执行,而是当做原始内容处理,用于把整段文本解释为纯字符数据而不是标记。这就要用到CDATA节。

语法如下:

<![CDATA[
    ......
]]>
Copy after login
Copy after login

CDATA节中可以输入任意字符(除]]>外),但是不能嵌套!

如下例,这种情况它不会报错,而如果不包含在CDATA节中,就会报错:

<stu id="001">
    <name>杨过</name> 
    <sex>男</sex>
    <age>20</age>
    <intro><![CDATA[ad<<&$^#*k]]></intro></stu>
Copy after login
Copy after login

4.6.XML语法-处理指令

处理指令,简称PI(processing instruction)。处理指令用来指示解析引擎如何解析XML文件,看下面一个例子:

比如我们也可以使用css样式表来修饰XML文件,编写my.css如下:

name{    
font-size:80px;    
font-weight:bold;    
color:red;
}
sex{    
font-size:60px;    
font-weight:bold;    
color:blue;
}
sex{    
font-size:40px;    
font-weight:bold;    
color:green;
}
Copy after login
Copy after login

我们在xml文件中使用处理指令引入这个css文件,如下:

<?xml version="1.0" encoding="gb2312"?><?xml-stylesheet href="my.css" type="text/css"?><class>
    <stu id="001">
        <name>杨过</name> 
        <sex>男</sex>
        <age>20</age>
    </stu>  
    <stu id="002">
        <name>小龙女</name>    
        <sex>女</sex>
        <age>21</age>
    </stu></class>
Copy after login

这时候我们再用浏览器打开这个xml文件,会发现浏览器解析出一个带样式的视图,而不再是单纯的目录树了:

XML—XML introduction and basic syntax

但是XML的处理指令不要求掌握,因为用到的很少。

5.格式正规的XML文档-小结

语法规范

  1. XML声明语句
    2.必须有一个根元素
    3.标记大小写敏感
    4.属性值用引号
    5.标记成对
    6.空标记关闭
    7.元素正确嵌套

 以上就是XML—XML介绍和基本语法的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!