gml(1969)->sgml(1985)->html(1993)- >xml(1998)
html The language itself has some flaws(1) Tags cannot be customized
There is an intermediate transition language, xhtml:
(2) HTML itself lacks meaning
(3) HTML is not truly internationalizedhtml->xhtml->xml
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.
2.XML can be used as configuration files
3.XML can be used as a small database
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>
We can open it with a browser:
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.4.XML syntaxThe solution is to change the XML file to UTF-8 encoding mode.
2.Element
3.Attributes
4.Comments
5.CDATA area, special characters
6.Processing instruction
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
encoding – document character encoding, such as “GB2312” or “UTF-8”4.2. XML syntax - element (or tag, node)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
root element
<a>www.sohu.com</a>
<a></a>,简写为:<a/>
will definitely result in an error.<a>welcome to <b> www.sohu.com </a></b>Copy after loginCopy after login
(4)对于XML标签中出现的所有空格和换行,XML解析程序都会当做标签内容进行处理。例如下面两段内容的意义是不一样的。
<stu>xiaoming</stu>
和如下:
<stu> xiaoming</stu>
(5)由于在XML中,空格和换行都作为原始内容被处理,所以,在编写XML文件时,要特别注意。
(6)命名规范:一个XML元素可以包含字母、数字以及其它一些可见字符,但必须遵守以下规范:
区分大小写,例如,元素P和元素p是两个不同的元素
不能以数字或下划线”_”开头
元素内不能包含空格
名称中间不能包含冒号(:)
可以使用中文,但一般不这么用
<student id="100"> <name>Tom</name></student>
(1)属性值用双引号(”)或单引号(’)分隔,如果属性值中有单引号,则用双引号分隔;如果有双引号,则用单引号分隔。那么如果属性值中既有单引号还有双引号怎么办?这种要使用实体(转义字符,类似于html中的空格符),XML有5个预定义的实体字符,如下:
(2)一个元素可以有多个属性,它的基本格式为:
<元素名 属性名1="属性值1" 属性名2="属性值2">
(3)特定的属性名称在同一个元素标记中只能出现一次
(4)属性值不能包括<,>,&,如果一定要包含,也要使用实体
XML的注释类似于HTML中的注释:
<!--这是一个注释-->
(1)注释内容不要出现
--
(2)不要把注释放在标记中间;
(3)注释不能嵌套
(4)可以在除标记以外的任何地方放注释
假如有这么一个需求,需要通过XML文件传递一幅图片,怎么做呢?其实我们看到的电脑上的所有文件,本质上都是字符串,不过它们都是特殊的二进制字符串。我们可以通过XML文件将一幅图片的二进制字符串传递过去,然后再解析成一幅图片。那么这个字符串就会包含大量的<,>,&或者“等一些特殊的不合法的字符。这时候解析引擎是会报错的。
所以,有些内容可能不想让解析引擎解析执行,而是当做原始内容处理,用于把整段文本解释为纯字符数据而不是标记。这就要用到CDATA节。
语法如下:
<![CDATA[ ...... ]]>
CDATA节中可以输入任意字符(除]]>
外),但是不能嵌套!
如下例,这种情况它不会报错,而如果不包含在CDATA节中,就会报错:
<stu id="001"> <name>杨过</name> <sex>男</sex> <age>20</age> <intro><![CDATA[ad<<&$^#*k]]></intro></stu>
处理指令,简称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; }
我们在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>
这时候我们再用浏览器打开这个xml文件,会发现浏览器解析出一个带样式的视图,而不再是单纯的目录树了:
但是XML的处理指令不要求掌握,因为用到的很少。
语法规范:
1.XML声明语句
2.必须有一个根元素
3.标记大小写敏感
4.属性值用引号
5.标记成对
6.空标记关闭
7.元素正确嵌套
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 可扩展标记语言
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.
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>
We can open it with a browser:
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.
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
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
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
( 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>
without tag body:
<a></a>,简写为:<a/>
(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>
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>
and as follows:
<stu> xiaoming</stu>
(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
<student id="100"> <name>Tom</name></student>
(1)属性值用双引号(”)或单引号(’)分隔,如果属性值中有单引号,则用双引号分隔;如果有双引号,则用单引号分隔。那么如果属性值中既有单引号还有双引号怎么办?这种要使用实体(转义字符,类似于html中的空格符),XML有5个预定义的实体字符,如下:
(2)一个元素可以有多个属性,它的基本格式为:
<元素名 属性名1="属性值1" 属性名2="属性值2">
(3)特定的属性名称在同一个元素标记中只能出现一次
(4)属性值不能包括<,>,&,如果一定要包含,也要使用实体
XML的注释类似于HTML中的注释:
<!--这是一个注释-->
(1)注释内容不要出现
--
(2)不要把注释放在标记中间;
(3)注释不能嵌套
(4)可以在除标记以外的任何地方放注释
假如有这么一个需求,需要通过XML文件传递一幅图片,怎么做呢?其实我们看到的电脑上的所有文件,本质上都是字符串,不过它们都是特殊的二进制字符串。我们可以通过XML文件将一幅图片的二进制字符串传递过去,然后再解析成一幅图片。那么这个字符串就会包含大量的<,>,&或者“等一些特殊的不合法的字符。这时候解析引擎是会报错的。
所以,有些内容可能不想让解析引擎解析执行,而是当做原始内容处理,用于把整段文本解释为纯字符数据而不是标记。这就要用到CDATA节。
语法如下:
<![CDATA[ ...... ]]>
CDATA节中可以输入任意字符(除]]>
外),但是不能嵌套!
如下例,这种情况它不会报错,而如果不包含在CDATA节中,就会报错:
<stu id="001"> <name>杨过</name> <sex>男</sex> <age>20</age> <intro><![CDATA[ad<<&$^#*k]]></intro></stu>
处理指令,简称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; }
我们在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>
这时候我们再用浏览器打开这个xml文件,会发现浏览器解析出一个带样式的视图,而不再是单纯的目录树了:
但是XML的处理指令不要求掌握,因为用到的很少。
语法规范:
XML声明语句
2.必须有一个根元素
3.标记大小写敏感
4.属性值用引号
5.标记成对
6.空标记关闭
7.元素正确嵌套
以上就是XML—XML介绍和基本语法的内容,更多相关内容请关注PHP中文网(www.php.cn)!