Crazy XML study notes (6) -----------XML expansion

黄舟
Release: 2017-02-21 14:33:47
Original
1503 people have browsed it

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:

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:

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:

or this way:

Note: If the attribute value itself contains double quotes, it is necessary to surround it with single quotes. Like this example:

Or you can use entity references:

XML Elements vs. Attributes

Please see These examples:

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:

The second example uses the date element:

The third example uses the extended date element (this is my favorite) :

Avoid XML attributes?

Some problems caused by using attributes:

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

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:


  
    George
    John
    Reminder
    Don't forget the meeting!
  
  
    John
    George
    Re: Reminder
    I will not
   
Copy after login

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 document

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

  • 
    
    George
    John
    Reminder
    Don't forget the meeting!
    
    Copy after login

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


George
John
Reminder
Don't forget the meeting!
Copy after login

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:


  
  
  
  
]>
Copy after login

Summary of DTD:

http://www.php.cn/

XML Schema




  
    
    
    
    
  


Copy after login

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 表格行:







Copy after login

例子解释:

  1. 检测浏览器,然后使用合适的解析器来加载 XML

  2. 创建一个 HTML 表格(

  3. 使用 getElementsByTagName() 来获得所有 XML 的 CD 节点

  4. 针对每个 CD 节点,把 ARTIST 和 TITLE 中的数据显示为表格数据

  5. 结束表格

XMLHttpRequest

什么是 XMLHttpRequest 对象?

XMLHttpRequest 对象是开发者的梦想,因为您能够:

  • 在不重新加载页面的情况下更新网页

  • 在页面已加载后从服务器请求数据

  • 在页面已加载后从服务器接收数据

  • 在后台向服务器发送数据

所有现代的浏览器都支持 XMLHttpRequest 对象。

实例:当键入文本时与服务器进行 XML HTTP 通信

创建 XMLHttpRequest 对象

通过一行简单的 JavaScript 代码,我们就可以创建 XMLHttpRequest 对象。

在所有现代浏览器中(包括 IE 7):

xmlhttp=new XMLHttpRequest()
Copy after login

在 Internet Explorer 5 和 6 中:

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
Copy after login

实例

Copy after login

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 目录:



  
    Empire Burlesque
    Bob Dylan
    USA
    Columbia
    10.90
    1985
  
.
.
... more ...
.
Copy after login

加载 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");
Copy after login

在本代码执行后,xmlDoc 成为一个 XML DOM 对象,可由 JavaScript 访问。

把 XML 数据显示为 HTML 表格

以下代码使用来自 XML DOM 对象的数据来填充一个 HTML 表格:

document.write("");

var x=xmlDoc.getElementsByTagName("CD");
for (var i=0;i");
document.write("");

document.write("");
document.write("");
}
document.write("
"); document.write( x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); document.write(""); document.write( x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); document.write("
");
Copy after login

针对 XML 文档中的每个 CD 元素,会创建一个表格行。每个表格行包含两个表格数据单元,其中的数据来自当前 CD 元素的 ARTIST 和 TITLE。

在任意 HTML 元素中显示 XML 数据

XML 数据可以拷贝到任何有能力显示文本的 HTML 元素。

下面的代码为 HTML 文件的 部分。这段代码从第一个 元素中获得 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+"
Title: "+title+"
Year: "+year; document.getElementById("show").innerHTML=txt; }
Copy after login

HTML 的 body 元素包含一个 onload 事件属性,它的作用是在页面已经加载时调用 display() 函数。body 元素中还包含了供接受 XML 数据的

元素。

Copy after login

通过上例,你只能看到来自 XML 文档中第一个 CD 元素中的数据。为了导航到数据的下一行,必须添加更多的代码。

添加导航脚本

为了向上例添加导航(功能),需要创建 next() 和 previous() 两个函数:

function next()
{
if (i0)
  {
  i--;
  display();
  }
}
Copy after login

next() 函数确保已到达最后一个 CD 元素后不显示任何东西,previous () 函数确保已到达第一个 CD 元素后不显示任何东西。

通过点击 next/previous 按钮来调用 next() 和 previous() 函数:


Copy after login

 

以上就是疯狂XML学习笔记(6)-----------XML拓展的内容,更多相关内容请关注PHP中文网(www.php.cn)!

 

 

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!