The basic knowledge and format requirements of XML will not be discussed in detail here
The following is a summary of the basics of XML
First acquaintance
http://www.php.cn/
Basics
http://www.php.cn/
The following is an extension to XML including XML attributes and validation
XML elements can contain attributes in the opening tag, similar to HTML.
Attributes provide additional information about the element.
XML Attributes
From HTML, you'll recall this: . The "src" attribute provides additional information about the
element.
In HTML (and in XML), attributes provide additional information about an element:
<img src="/static/imghwm/default1.png" data-src="computer.gif" class="lazy" alt="Crazy XML study notes (6) -----------XML expansion" > <a href="demo.asp">
Attributes often provide information that is not part of the data. In the following example, the file type is irrelevant to the data, but is important to the software that needs to process this element:
<file type="gif">computer.gif</file>
XML attributes must be quoted
Attribute values must be surrounded by quotes, but both single and double quotes can be used. For example, for a person's gender, the person tag can be written like this:
<person sex="female">
or this way:
<person sex='female'>
Note: If the attribute value itself contains double quotes, it is necessary to surround it with single quotes. Like this example:
<gangster name='George "Shotgun" Ziegler'>
Or you can use entity references:
<gangster name="George "Shotgun" Ziegler">
XML Elements vs. Attributes
Please see These examples:
<person sex="female">Anna Smith female Anna Smith
In the first example, sex is an attribute. In the second example, sex is a child element. Both examples provide the same information.
There are no rules that tell us when to use attributes and when to use subelements. My experience is that in HTML, attributes are convenient to use, but in XML, you should try to avoid using attributes. If the information feels a lot like data, use child elements.
My favorite way
The following three XML documents contain exactly the same information:
The first one The example uses the date attribute:
<note date="08/08/2008"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
The second example uses the date element:
<note> <date>08/08/2008</date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
The third example uses the extended date element (this is my favorite) :
<note> <date> <day>08</day> <month>08</month> <year>2008</year> </date> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
Avoid XML attributes?
Some problems caused by using attributes:
Attributes cannot contain multiple values (elements can)
Attributes cannot describe the tree structure (elements can)
Attributes are not easily extensible (for future changes)
Attributes are difficult to read And maintenance
Please try to use elements to describe data. Instead, just use attributes to provide data-independent information.
Don't do something stupid like this (this is not the way XML should be used):
<note day="08" month="08" year="2008" to="George" from="John" heading="Reminder" body="Don't forget the meeting!"> </note>
XML attributes for metadata
Sometimes ID references are assigned to elements. These ID indexes can be used to identify XML elements in the same way as the ID attribute in HTML. This example demonstrates this to us:
<messages> <note id="501"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note> <note id="502"> <to>John</to> <from>George</from> <heading>Re: Reminder</heading> <body>I will not</body> </note> </messages>
The ID above is just an identifier, used to identify different notes. It is not part of the note data.
The idea we are trying to convey to you here is that metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.
XML Validation
## XML with correct syntax is called "well-formed" XML.
XML that has been validated against a DTD is "valid" XML.
Well-formed XML documentA "well-formed" XML document has correct syntax.
A "well-formed" XML document will obey the XML syntax rules introduced in the previous chapters:- The XML document must have a root element
- XML documents must have closing tags
- XML tags are case-sensitive
- XML elements must be correct Nested
- XML attributes must be quoted
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>Validate XML document A valid XML document is a "well-formed" XML document that also adheres to the syntax rules of the Document Type Definition (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>In the above example, the DOCTYPE declaration is for an external DTD file Quote. The following paragraphs show the contents of this file. XML DTD The role of DTD is to define the structure of an XML document. It uses a series of legal elements to define the document structure:
<!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]>Summary of DTD: http://www.php.cn/ 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>A universal validator
XML 错误会终止您的程序
XML 文档中的错误会终止你的 XML 程序。
W3C 的 XML 规范声明:如果 XML 文档存在错误,那么程序就不应当继续处理这个文档。理由是,XML 软件应当轻巧,快速,具有良好的兼容性。
如果使用 HTML,创建包含大量错误的文档是有可能的(比如你忘记了结束标签)。其中一个主要的原因是 HTML 浏览器相当臃肿,兼容性也很差,并且它们有自己的方式来确定当发现错误时文档应该显示为什么样子。
使用 XML 时,这种情况不应当存在。
对您的 XML 进行语法检查 - 仅用于 IE 浏览器
为了帮助您对 XML 进行语法检查,我们创建了一个 XML 验证器。
把您的 XML 粘贴到下面的文本框中,然后点击"验证"按钮来进行语法检查。
根据 DTD 来验证 XML
只要把 DOCTYPE 声明添加到您的 XML 中,然后点击验证按钮即可:
注释:只有在 Internet Explorer 中,可以根据 DTD 来验证 XML。Firefox, Mozilla, Netscape 以及 Opera 做不到这一点。
如何将XML显示在Html上:
实例
从 XML 文件中加载数据,然后把数据显示为一个 HTML 表格
在 HTML 中显示数据
在上一节中,我们讲解了如何通过 JavaScript 来解析 XML 并访问 DOM。
本例遍历一个 XML 文件 (cd_catalog.xml),然后把每个 CD 元素显示为一个 HTML 表格行:
<html> <body> <script type="text/javascript"> var xmlDoc=null; if (window.ActiveXObject) {// code for IExmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } else if (document.implementation.createDocument) {// code for Mozilla, Firefox, Opera, etc.xmlDoc=document.implementation.createDocument("","",null); } else { alert('Your browser cannot handle this script'); } if (xmlDoc!=null) { xmlDoc.async=false; xmlDoc.load("cd_catalog.xml"); document.write("<table border='1'>"); var x=xmlDoc.getElementsByTagName("CD"); for (i=0;i<x.length;i++) { document.write("<tr>"); document.write("<td>"); document.write( x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); document.write("</td>"); document.write("<td>"); document.write( x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); document.write("</td>"); document.write("</tr>"); } document.write("</table>"); } </script> </body> </html>
例子解释:
检测浏览器,然后使用合适的解析器来加载 XML
-
创建一个 HTML 表格(
)
结束表格使用 getElementsByTagName() 来获得所有 XML 的 CD 节点
针对每个 CD 节点,把 ARTIST 和 TITLE 中的数据显示为表格数据
用
XMLHttpRequest
什么是 XMLHttpRequest 对象?
XMLHttpRequest 对象是开发者的梦想,因为您能够:
在不重新加载页面的情况下更新网页
在页面已加载后从服务器请求数据
在页面已加载后从服务器接收数据
在后台向服务器发送数据
所有现代的浏览器都支持 XMLHttpRequest 对象。
实例:当键入文本时与服务器进行 XML HTTP 通信。
创建 XMLHttpRequest 对象
通过一行简单的 JavaScript 代码,我们就可以创建 XMLHttpRequest 对象。
在所有现代浏览器中(包括 IE 7):
xmlhttp=new XMLHttpRequest()
在 Internet Explorer 5 和 6 中:
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
实例
TIY
注释:onreadystatechange 是一个事件句柄。它的值 (state_Change) 是一个函数的名称,当 XMLHttpRequest 对象的状态发生改变时,会触发此函数。状态从 0 (uninitialized) 到 4 (complete) 进行变化。仅在状态为 4 时,我们才执行代码。
为什么使用 Async=true ?
我们的实例在 open() 的第三个参数中使用了 "true"。
该参数规定请求是否异步处理。
True 表示脚本会在 send() 方法之后继续执行,而不等待来自服务器的响应。
onreadystatechange 事件使代码复杂化了。但是这是在没有得到服务器响应的情况下,防止代码停止的最安全的方法。
通过把该参数设置为 "false",可以省去额外的 onreadystatechange 代码。如果在请求失败时是否执行其余的代码无关紧要,那么可以使用这个参数。
XML实例:
XML 文档实例
请看下面这个 XML 文档 ( "cd_catalog.xml" ),它描述了一个 CD 目录:
<?xml version="1.0" encoding="ISO-8859-1"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> . . ... more ... .
加载 XML 文档
为了加载 XML 文档 (cd_catalog.xml),我们使用了与 XML 解析器那一节中相同的代码:
var xmlDoc; if (window.ActiveXObject) { // code for IE xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } else if (document.implementation.createDocument) { // code for Firefox, Mozilla, Opera, etc. xmlDoc=document.implementation.createDocument("","",null); } else { alert('Your browser cannot handle this script'); } xmlDoc.async=false; xmlDoc.load("cd_catalog.xml");
在本代码执行后,xmlDoc 成为一个 XML DOM 对象,可由 JavaScript 访问。
把 XML 数据显示为 HTML 表格
以下代码使用来自 XML DOM 对象的数据来填充一个 HTML 表格:
document.write("<table border='1'>"); var x=xmlDoc.getElementsByTagName("CD"); for (var i=0;i<x.length;i++) { document.write("<tr>"); document.write("<td>"); document.write( x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); document.write("</td>"); document.write("<td>"); document.write( x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); document.write("</td>"); document.write("</tr>"); } document.write("</table>");
针对 XML 文档中的每个 CD 元素,会创建一个表格行。每个表格行包含两个表格数据单元,其中的数据来自当前 CD 元素的 ARTIST 和 TITLE。
在任意 HTML 元素中显示 XML 数据
XML 数据可以拷贝到任何有能力显示文本的 HTML 元素。
下面的代码为 HTML 文件的 部分。这段代码从第一个
var x=xmlDoc.getElementsByTagName("CD"); i=0; function display() { artist= (x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); title= (x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); year= (x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue); txt="Artist: "+artist+"<br />Title: "+title+"<br />Year: "+year; document.getElementById("show").innerHTML=txt; }
HTML 的 body 元素包含一个 onload 事件属性,它的作用是在页面已经加载时调用 display() 函数。body 元素中还包含了供接受 XML 数据的 元素。
<p id='show'></p> </body>
通过上例,你只能看到来自 XML 文档中第一个 CD 元素中的数据。为了导航到数据的下一行,必须添加更多的代码。
添加导航脚本
为了向上例添加导航(功能),需要创建 next() 和 previous() 两个函数:
function next() { if (i<x.length-1) { i++; display(); } } function previous() { if (i>0) { i--; display(); } }
next() 函数确保已到达最后一个 CD 元素后不显示任何东西,previous () 函数确保已到达第一个 CD 元素后不显示任何东西。
通过点击 next/previous 按钮来调用 next() 和 previous() 函数:
<input type="button" onclick="previous()" value="previous" /> <input type="button" onclick="next()" value="next" />
以上就是疯狂XML学习笔记(6)-----------XML拓展的内容,更多相关内容请关注PHP中文网(www.php.cn)!

XML has the advantages of structured data, scalability, cross-platform compatibility and parsing verification in RSS. 1) Structured data ensures consistency and reliability of content; 2) Scalability allows the addition of custom tags to suit content needs; 3) Cross-platform compatibility makes it work seamlessly on different devices; 4) Analytical and verification tools ensure the quality and integrity of the feed.

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

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