Home Backend Development XML/RSS Tutorial Detailed introduction to XML principle code examples

Detailed introduction to XML principle code examples

Mar 31, 2017 pm 02:18 PM

XML 简介 
XML 被设计用来传输和存储数据。类似于JSON。 
XML 指可扩展标记语言(EXtensible Markup Language) 
XML 是一种标记语言,很类似 HTML 
XML 的设计宗旨是传输数据,而非显示数据 
XML 标签没有被预定义。您需要自行定义标签。 
XML 被设计为具有自我描述性。 
XML 是 W3C 的推荐标准 
XML 被设计用来结构化、存储以及传输信息。(没有格式双方很难知道信息的结构内容) 
实例: 

<?xml version="1.0" encoding="utf-8"?> 

<note> 
  <to>George</to> 
  <from>John</from> 
  <heading>Reminder</heading> 
  <body>Don&#39;t forget the meeting!</body> 
</note>
Copy after login

实例解释:
第一行是 XML 声明。它定义 XML 的版本 (1.0) 和所使用的编码 (ISO-8859-1 = Latin-1/西欧字符集)。
描述文档的根元素

<note>//根元素的开始 
  ** 
</note> //根元素的结尾
Copy after login

4个子元素

<to>George</to> 
<from>John</from> 
<heading>Reminder</heading> 
<body>Don&#39;t forget the meeting!</body>
Copy after login

就这样XML文档会形成一种树结构,如下:

<?xml version="1.0" encoding="utf-8"?> 

<note> 
  <to>George</to> 
  <from>John</from> 
  <heading>Reminder</heading> 
  <body> 
    <A>George</A> 
    <B>John</B> 
    <C>Reminder</C> 
  </body> 
</note>
Copy after login

所有 XML 元素都须有关闭标签

<p>This is a paragraph	//错 
<p>This is a paragraph</p>	//对
Copy after login

XML 标签对大小写敏感

<Message>这是错误的。</message> //错 
<message>这是正确的。</message> //对
Copy after login

XML 必须正确地嵌套

<b><i>This text is bold and italic</b></i>	//错 
<b><i>This text is bold and italic</i></b>	//对
Copy after login

XML 的属性值须加引号

<note date=08/08/2008>	//错 
  <to>George</to> 
  <from>John</from> 
</note> 

<note date="08/08/2008"> //对 
  <to>George</to> 
  <from>John</from> 
</note>
Copy after login

实体引用:就是特殊字符的转意。
< < 小于
> > 大于
& & 和号
' ' 单引号
" " 引号

<message>if salary &lt; 1000 then</message>
Copy after login

想要的:

<message>if salary < 1000 then</message>
Copy after login

XML 中的注释

<!-- This is a comment -->
Copy after login

在 XML 中,空格会被保留

HTML 会把多个连续的空格字符裁减(合并)为一个:

HTML:Hello           my name is David. 
输出:Hello my name is David.
Copy after login

在 XML 中,文档中的空格不会被删节。

XML 元素
开始标签直到(且包括)结束标签的部分。如:

<from>John</from>
Copy after login

元素可包含其他元素、文本或者两者的混合物。元素也可以拥有属性。如:

<book category="CHILDREN">	//category(属性) 
  <title>Harry Potter</title> //book的子元素,这个子元素只有文本内容 
  <author>J K. Rowling</author> 
  <year>2005</year> 
  <price>29.99</price> 
</book>
Copy after login

XML 命名规则

XML 元素必须遵循以下命名规则:
名称可以含字母、数字以及其他的字符
名称不能以数字或者标点符号开始
名称不能以字符 “xml”(或者 XML、Xml)开始
名称不能包含空格

XML 元素 vs. 属性

<person sex="female">	//属性 
  <firstname>Anna</firstname> 
  <lastname>Smith</lastname> 
</person> 

<person> 
  <sex>female</sex>	//元素 
  <firstname>Anna</firstname> 
  <lastname>Smith</lastname> 
</person>
Copy after login

没有什么规矩可以告诉我们什么时候该使用属性,而什么时候该使用子元素,在 XML 中,
您应该尽量避免使用属性。如果信息感觉起来很像数据,那么请使用子元素吧。

属性无法包含多重的值(元素可以)
属性无法描述树结构(元素可以)
属性不易扩展(为未来的变化)
属性难以阅读和维护

XML 命名空间(XML Namespaces)

XML 命名空间提供避免元素命名冲突的方法。
这个 XML 文档携带着某个表格中的信息:

<table> 
   <tr> 
   <td>Apples</td> 
   <td>Bananas</td> 
   </tr> 
</table>
Copy after login

这个 XML 文档携带有关桌子的信息(一件家具):

<table> 
   <name>African Coffee Table</name> 
   <width>80</width> 
   <length>120</length> 
</table>
Copy after login

由于两个文档都包含带有不同内容和定义的

元素,就会发生命名冲突。

使用前缀来避免命名冲突

<h:table> 
   <h:tr> 
   <h:td>Apples</h:td> 
   <h:td>Bananas</h:td> 
   </h:tr> 
</h:table> 

<f:table> 
   <f:name>African Coffee Table</f:name> 
   <f:width>80</f:width> 
   <f:length>120</f:length> 
</f:table>
Copy after login

使用命名空间(Namespaces)

<h:table xmlns:h="http://www.w3.org/TR/html4/"> 
   <h:tr> 
   <h:td>Apples</h:td> 
   <h:td>Bananas</h:td> 
   </h:tr> 
</h:table>
Copy after login

此 XML 文档携带着有关一件家具的信息:

<f:table xmlns:f="http://www.w3school.com.cn/furniture"> 
   <f:name>African Coffee Table</f:name> 
   <f:width>80</f:width> 
   <f:length>120</f:length> 
</f:table>
Copy after login

默认的命名空间(Default Namespaces)

为元素定义默认的命名空间可以让我们省去在所有的子元素中使用前缀的工作。

<table xmlns="http://www.w3.org/TR/html4/"> 
   <tr> 
   <td>Apples</td> 
   <td>Bananas</td> 
   </tr> 
</table>
Copy after login

此 XML 文档携带着有关一件家具的信息:

<table xmlns="http://www.w3school.com.cn/furniture"> 
   <name>African Coffee Table</name> 
   <width>80</width> 
   <length>120</length> 
</table>
Copy after login

命名空间是就近原则的

<?xml version="1.0" encoding="utf-8"?> 
<root xmlns="dotnet" xmlns:w="wpf"> 
  <!-- xmlns: dotnet --> 
  <a>data in a</a>	//默认的命名空间 
  <!-- xmlns: dotnet --> 
  <w:b>data in b</w:b>	//w命名空间 
  <!-- xmlns: wpf --> 
  <c xmlns="silverlight"> 
    <!-- xmlns: silverlight --> 
    <w:d> 
      <!-- xmlns: wpf --> 
      <e>data in e</e> 
      <!-- xmlns: silverlight -->	//就近原则 
    </w:d> 
  </c> 
</root>
Copy after login

CDATA
术语 CDATA 指的是不应由 XML 解析器进行解析的文本数据(Unparsed Character Data)。(就是里面的数据不进行XML解析)
CDATA 部分由 "" 结束:

<script> 
<![CDATA[	//开始 
function matchwo(a,b) 
{ 
if (a < b && a < 0) then 
  { 
  return 1; 
  } 
else 
  { 
  return 0; 
  } 
} 
]]>	//结束 
</script>
Copy after login

The above is the detailed content of Detailed introduction to XML principle code examples. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
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

Analysis of the function and principle of nohup Analysis of the function and principle of nohup Mar 25, 2024 pm 03:24 PM

Analysis of the role and principle of nohup In Unix and Unix-like operating systems, nohup is a commonly used command that is used to run commands in the background. Even if the user exits the current session or closes the terminal window, the command can still continue to be executed. In this article, we will analyze the function and principle of the nohup command in detail. 1. The role of nohup: Running commands in the background: Through the nohup command, we can let long-running commands continue to execute in the background without being affected by the user exiting the terminal session. This needs to be run

In-depth discussion of the principles and practices of the Struts framework In-depth discussion of the principles and practices of the Struts framework Feb 18, 2024 pm 06:10 PM

Principle analysis and practical exploration of the Struts framework. As a commonly used MVC framework in JavaWeb development, the Struts framework has good design patterns and scalability and is widely used in enterprise-level application development. This article will analyze the principles of the Struts framework and explore it with actual code examples to help readers better understand and apply the framework. 1. Analysis of the principles of the Struts framework 1. MVC architecture The Struts framework is based on MVC (Model-View-Con

In-depth understanding of the batch Insert implementation principle in MyBatis In-depth understanding of the batch Insert implementation principle in MyBatis Feb 21, 2024 pm 04:42 PM

MyBatis is a popular Java persistence layer framework that is widely used in various Java projects. Among them, batch insertion is a common operation that can effectively improve the performance of database operations. This article will deeply explore the implementation principle of batch Insert in MyBatis, and analyze it in detail with specific code examples. Batch Insert in MyBatis In MyBatis, batch Insert operations are usually implemented using dynamic SQL. By constructing a line S containing multiple inserted values

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Detailed explanation of the principle of MyBatis paging plug-in Detailed explanation of the principle of MyBatis paging plug-in Feb 22, 2024 pm 03:42 PM

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

An in-depth discussion of the functions and principles of Linux RPM tools An in-depth discussion of the functions and principles of Linux RPM tools Feb 23, 2024 pm 03:00 PM

The RPM (RedHatPackageManager) tool in Linux systems is a powerful tool for installing, upgrading, uninstalling and managing system software packages. It is a commonly used software package management tool in RedHatLinux systems and is also used by many other Linux distributions. The role of the RPM tool is very important. It allows system administrators and users to easily manage software packages on the system. Through RPM, users can easily install new software packages and upgrade existing software

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

See all articles