Home > Article > Backend Development > Crazy XML study notes (6) -----------XML expansion
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.
From HTML, you'll recall this: 08c069fd51d2c2d81cee4e0f0ed64316 . The "src" attribute provides additional information about the a1f02c36ba31691bcfe87b2722de723b element.
In HTML (and in XML), attributes provide additional information about an element:
<img src="computer.gif"> <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>
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">
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.
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>
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>
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:
<?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 程序。
W3C 的 XML 规范声明:如果 XML 文档存在错误,那么程序就不应当继续处理这个文档。理由是,XML 软件应当轻巧,快速,具有良好的兼容性。
如果使用 HTML,创建包含大量错误的文档是有可能的(比如你忘记了结束标签)。其中一个主要的原因是 HTML 浏览器相当臃肿,兼容性也很差,并且它们有自己的方式来确定当发现错误时文档应该显示为什么样子。
使用 XML 时,这种情况不应当存在。
为了帮助您对 XML 进行语法检查,我们创建了一个 XML 验证器。
把您的 XML 粘贴到下面的文本框中,然后点击"验证"按钮来进行语法检查。
只要把 DOCTYPE 声明添加到您的 XML 中,然后点击验证按钮即可:
注释:只有在 Internet Explorer 中,可以根据 DTD 来验证 XML。Firefox, Mozilla, Netscape 以及 Opera 做不到这一点。
如何将XML显示在Html上:
从 XML 文件中加载数据,然后把数据显示为一个 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 表格(c3eabfe0f2ab8a9eadae05ced4d2507b)
使用 getElementsByTagName() 来获得所有 XML 的 CD 节点
针对每个 CD 节点,把 ARTIST 和 TITLE 中的数据显示为表格数据
用 f16b1740fad44fb09bfe928bcc527e08 结束表格
XMLHttpRequest
XMLHttpRequest 对象是开发者的梦想,因为您能够:
在不重新加载页面的情况下更新网页
在页面已加载后从服务器请求数据
在页面已加载后从服务器接收数据
在后台向服务器发送数据
所有现代的浏览器都支持 XMLHttpRequest 对象。
实例:当键入文本时与服务器进行 XML HTTP 通信。
通过一行简单的 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 时,我们才执行代码。
我们的实例在 open() 的第三个参数中使用了 "true"。
该参数规定请求是否异步处理。
True 表示脚本会在 send() 方法之后继续执行,而不等待来自服务器的响应。
onreadystatechange 事件使代码复杂化了。但是这是在没有得到服务器响应的情况下,防止代码停止的最安全的方法。
通过把该参数设置为 "false",可以省去额外的 onreadystatechange 代码。如果在请求失败时是否执行其余的代码无关紧要,那么可以使用这个参数。
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 文档 (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 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。
XML 数据可以拷贝到任何有能力显示文本的 HTML 元素。
下面的代码为 HTML 文件的 93f0f5c25f18dab9d176bd4f6de5d30e 部分。这段代码从第一个 d15114bf3dc8d8fe2e2887944c735e50 元素中获得 XML 数据,然后在 id="show" 的 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 数据的 a1862d91ae37e8ab9716d1c936937a12 元素。
<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)!