search
HomeBackend DevelopmentXML/RSS TutorialDetailed introduction to XML principle code examples

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>

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

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

4个子元素 

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

就这样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>

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

<p>This is a paragraph	//错 
<p>This is a paragraph</p>	//对

XML 标签对大小写敏感 

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

XML 必须正确地嵌套 

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

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>

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

<message>if salary < 1000 then</message>

想要的: 

<message>if salary < 1000 then</message>

XML 中的注释 

<!-- This is a comment -->

在 XML 中,空格会被保留 

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

HTML:Hello           my name is David. 
输出:Hello my name is David.

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

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

<from>John</from>

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

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

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>

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

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

XML 命名空间(XML Namespaces) 

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

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

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

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

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

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

使用前缀来避免命名冲突 

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

使用命名空间(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>

此 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>

默认的命名空间(Default Namespaces) 

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

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

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

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

命名空间是就近原则的 

<?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>

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>

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
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
RSS, XML and the Modern Web: A Content Syndication Deep DiveRSS, XML and the Modern Web: A Content Syndication Deep DiveMay 08, 2025 am 12:14 AM

RSS and XML are still important in the modern web. 1.RSS is used to publish and distribute content, and users can subscribe and get updates through the RSS reader. 2. XML is a markup language and supports data storage and exchange, and RSS files are based on XML.

Beyond Basics: Advanced RSS Features Enabled by XMLBeyond Basics: Advanced RSS Features Enabled by XMLMay 07, 2025 am 12:12 AM

RSS enables multimedia content embedding, conditional subscription, and performance and security optimization. 1) Embed multimedia content such as audio and video through tags. 2) Use XML namespace to implement conditional subscriptions, allowing subscribers to filter content based on specific conditions. 3) Optimize the performance and security of RSSFeed through CDATA section and XMLSchema to ensure stability and compliance with standards.

Decoding RSS: An XML Primer for Web DevelopersDecoding RSS: An XML Primer for Web DevelopersMay 06, 2025 am 12:05 AM

RSS is an XML-based format used to publish frequently updated data. As a web developer, understanding RSS can improve content aggregation and automation update capabilities. By learning RSS structure, parsing and generation methods, you will be able to handle RSSfeeds confidently and optimize your web development skills.

JSON vs. XML: Why RSS Chose XMLJSON vs. XML: Why RSS Chose XMLMay 05, 2025 am 12:01 AM

RSS chose XML instead of JSON because: 1) XML's structure and verification capabilities are better than JSON, which is suitable for the needs of RSS complex data structures; 2) XML was supported extensively at that time; 3) Early versions of RSS were based on XML and have become a standard.

RSS: The XML-Based Format ExplainedRSS: The XML-Based Format ExplainedMay 04, 2025 am 12:05 AM

RSS is an XML-based format used to subscribe and read frequently updated content. Its working principle includes two parts: generation and consumption, and using an RSS reader can efficiently obtain information.

Inside the RSS Document: Essential XML Tags and AttributesInside the RSS Document: Essential XML Tags and AttributesMay 03, 2025 am 12:12 AM

The core structure of RSS documents includes XML tags and attributes. The specific parsing and generation steps are as follows: 1. Read XML files, process and tags. 2. Extract,,, etc. tag information. 3. Handle custom tags and attributes to ensure version compatibility. 4. Use cache and asynchronous processing to optimize performance to ensure code readability.

JSON, XML, and Data Formats: Comparing RSSJSON, XML, and Data Formats: Comparing RSSMay 02, 2025 am 12:20 AM

The main differences between JSON, XML and RSS are structure and uses: 1. JSON is suitable for simple data exchange, with a simple structure and easy to parse; 2. XML is suitable for complex data structures, with a rigorous structure but complex parsing; 3. RSS is based on XML and is used for content release, standardized but limited use.

Troubleshooting XML/RSS Feeds: Common Pitfalls and Expert SolutionsTroubleshooting XML/RSS Feeds: Common Pitfalls and Expert SolutionsMay 01, 2025 am 12:07 AM

The processing of XML/RSS feeds involves parsing and optimization, and common problems include format errors, encoding issues, and missing elements. Solutions include: 1. Use XML verification tools to check for format errors; 2. Ensure encoding consistency and use the chardet library to detect encoding; 3. Use default values ​​or skip the element when missing elements; 4. Use efficient parsers such as lxml and cache parsing results to optimize performance; 5. Pay attention to data consistency and security to prevent XML injection attacks.

See all articles

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use