Table of Contents
1.XML History
An XML file is divided into the following parts:
Copy after login
Copy after login
The XML declaration is placed in the first line of the XML document " >
Copy after login
Copy after login
The XML declaration is placed in the first line of the XML document
4.3.XML语法-属性
4.4.XML语法-注释
4.5.XML语法-CDATA节
4.6.XML语法-处理指令
5.格式正规的XML文档-小结
1.XML历史
2. Why XML is needed
3. Common applications of XML
4.XML syntax
4.1.XML syntax-document declaration
4.2. XML syntax - element (or tag, node)
Home Backend Development XML/RSS Tutorial XML—XML introduction and basic syntax

XML—XML introduction and basic syntax

Feb 24, 2017 pm 02:53 PM

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
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
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)!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data Using Python to merge and deduplicate XML data Aug 07, 2023 am 11:33 AM

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Filtering and sorting XML data using Python Filtering and sorting XML data using Python Aug 07, 2023 pm 04:17 PM

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Import XML data into database using PHP Import XML data into database using PHP Aug 07, 2023 am 09:58 AM

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Python implements conversion between XML and JSON Aug 07, 2023 pm 07:10 PM

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

See all articles