


In addition to a detailed basic introduction to XML that Web programmers are concerned about
XML与HTML
首先,XML与HTML类似的,都是一种标记语言。
当初在设计XML时,并非为了将HTML赶下神坛,实际上,XML是为了另外一种目的设计的。
XML旨在传输信息,HTML旨在显示信息
HTML可以有什么标签,不能有什么标签,是被一系列规范约束的;
但在XML中,可以定义自己的标签。
XML有什么用
由于XML是纯文本格式的,因此独立于任何硬件和软件,是一种真正的跨平台数据传输格式。
在XML的基础上,许多其它的技术也得以诞生,比如我们最熟悉的web service,WSDL机制便是基于XML来实现的(也有基于JSON实现的)。
这都得益于XML是一种扩展性非常高的数据传输格式。
语法规则
必须有关闭标签
如<a>,则必须有一个对应的与之配对,当然用<a name=""/>则是一种简略的缩写。大小写敏感
XML文档必须有根元素
属性值必须加引号
-
特殊字符的转义
< - < > - > & - & ' - ' " - "
元素命名规则
名称可以含字母、数字以及其他的字符
名称不能以数字或者标点符号开始
名称不能以字符 “xml”(或者 XML、Xml)开始
名称不能包含空格
属性
属性必须加引号,如果属性本身有双引号,就用单引号包围它
<a name='steve "aplple" Jobs' />
XML验证
有许多验证方式来验证XML格式是否良好。常用的有以下两种:
XML DTD
XML Schema
XML DTD
合法的 XML 文档是“形式良好”的 XML 文档,同样遵守文档类型定义 (DTD) 的语法规则:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE note SYSTEM "Note.dtd"> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
在上例中,DOCTYPE 声明是对外部 DTD 文件的引用。下面的段落展示了这个文件的内容。
<!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]>
XML Schema
W3C 支持一种基于 XML 的 DTD 代替者,它名为 XML Schema:
<xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
XML命名空间
命名空间的主要目的是解决元素命名冲突的问题。以下两份XML配置文件有命名冲突的问题:
<table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>
<table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table>
使用命名空间来解决冲突后:
<table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>
<table xmlns="http://www.w3school.com.cn/furniture"> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table>
命名空间xmlns属性值本身并没有多大含义,只是为了区分命名空间的不同,但实际上xmlns会被开发者用来标识某些资源。
XML CDATA
所有 XML 文档中的文本均会被解析器解析。
只有 CDATA 区段(CDATA section)中的文本会被解析器忽略。
PCDATA
PCDATA 指的是被解析的字符数据(Parsed Character Data)。
XML 解析器通常会解析 XML 文档中所有的文本。
当某个 XML 元素被解析时,其标签之间的文本也会被解析:
<message>此文本也会被解析</message>
解析器之所以这么做是因为 XML 元素可包含其他元素,就像这个例子中,其中的
<name><first>Bill</first><last>Gates</last></name>
而解析器会把它分解为像这样的子元素:
<name> <first>Bill</first> <last>Gates</last> </name>
CDATA
术语 CDATA 指的是不应由 XML 解析器进行解析的文本数据(Unparsed Character Data)。
在 XML 元素中,"
"
"&" 也会产生错误,因为解析器会把该字符解释为字符实体的开始。
某些文本,比如 JavaScript 代码,包含大量 "
CDATA 部分中的所有内容都会被解析器忽略。
CDATA 部分由 " 开始,由 "]]>" 结束:
<script> <![CDATA[ function matchwo(a,b) { if (a < b && a < 0) then { return 1; } else { return 0; } } ]]> </script>
在上面的例子中,解析器会忽略 CDATA 部分中的所有内容。
关于 CDATA 部分的注释:
CDATA 部分不能包含字符串 "]]>"。也不允许嵌套的 CDATA 部分。
标记 CDATA 部分结尾的 "]]>" 不能包含空格或折行。
The above is the detailed content of In addition to a detailed basic introduction to XML that Web programmers are concerned about. For more information, please follow other related articles on the PHP Chinese website!

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.

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.

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

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.

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.

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.

The steps to parse RSS documents include: 1. Read the XML file, 2. Use DOM or SAX to parse XML, 3. Extract headings, links and other information, and 4. Process data. RSS documents are XML-based formats used to publish updated content, structures containing, and elements, suitable for building RSS readers or data processing tools.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)
