当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > DOM 解析

XML DOM
DOM 节点
DOM 节点列表
DOM 解析
DOM 遍历节点树
DOM Mozilla 和 IE
DOM 获取节点
DOM 设置节点
DOM 删除节点
DOM 更换节点
DOM 建立节点
DOM 添加节点
DOM 克隆节点
DOM 节点类型
DOM Node
DOM NodeList
DOM NamedNodeMap
DOM Document
DOM DocumentType
DOM ProcessingInstr
DOM Element

XML DOM 中的 DOM 解析


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1262 ::
收藏到网摘: n/a

To read and update, create and manipulate an XML document, you will need an XML parser.
你需要通过一个XML解析器来阅读、更新、创建和操作一份XML文档。


Examples
案例

Parse an XML file - Crossbrowser example
XML文件解析– Crossbrowser 案例
This example is a cross-browser example that loads an existing XML document ("note.xml") into the XML parser.
这个案例是一个跨浏览器的案例,它将一个现有的XML文档("note.xml")加载到XML解析器中去。

Parse an XML string - Crossbrowser example
XML 字符串解析 – Crossbrowser 案例
This example is a cross-browser example on how to load and parse an XML string.
这个案例是一个跨浏览器的案例,它解释了加载并解析一个XML字符串的方法。


Parsing the XML DOM
解析XML DOM

To manipulate an XML document, you need an XML parser. The parser loads the document into your computer's memory. Once the document is loaded, its data can be manipulated using the DOM. The DOM treats the XML document as a tree.
要熟练操作XML文档,你需要一个XML解析器。这个解析器会将文档加载到你的计算机存储器中。当文档加载完毕后,你只需要通过DOM就可以对它的数据进行操作。DOM是显示的是XML文档的树状结。

There are some differences between Microsoft's XML parser and the XML parser used in Mozilla browsers. In this tutorial we will show you how to create cross browser scripts that will work in both Internet Explorer and Mozilla browsers.
微软的XML解析器和Mozilla浏览器的XML解析器有点不同。在这份教程中,我们会说明如何在IE和Mozilla中创建跨浏览器的脚本程序。


Microsoft's XML Parser
微软的XML解析器

Microsoft's XML parser is a COM component that comes with Internet Explorer 5 and higher. Once you have installed Internet Explorer, the parser is available to scripts.
微软的XML解析器是和IE5以及更高版本浏览器合在一起的COM组件;一旦你安装了IE,这个解析器就会对脚本程序起作用了。

Microsoft's XML parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML.
微软的XML解析器支持所有可以贯穿节点树的必要函数,从而访问这些节点和属性值,插入或删除节点,或者重新把节点树转换为XML文件。

To create an instance of Microsoft's XML parser, use the following code:
如果你需要创建一个微软XML解析器,那么,需要使用下述代码:

JavaScript 代码:

var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

VBScript 代码:

set xmlDoc=CreateObject("Microsoft.XMLDOM")

ASP 代码:

set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")

The following code fragment loads an existing XML document ("note.xml") into Microsoft's XML parser:
下述代码片断将一个现有的XML文档("note.xml")加载到微软的XML解析器中:

var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("note.xml");

The first line of the script above creates an instance of the XML parser. The second line turns off asynchronized loading, to make sure that the parser will not continue execution of the script before the document is fully loaded. The third line tells the parser to load an XML document called "note.xml".
上述第一行的脚本程序创建了一个XML解析器的案例;第二行取消了同步加载,以保证文档在完全加载之前,避免解析器继续执行脚本程序;第三行语句指定解析器加载一个名为“note.xml”的XML文档。


XML Parser in Mozilla, Firefox, and Opera
Mozilla, Firefox, 和Opera中的 XML解析器

Mozilla's XML parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML.
Mozilla 的 XML解析器支持所有贯穿整个节点树的必要函数,从而可以访问节点及节点属性,插入或删除节点,以及将节点树重新转换为XML文档。

To create an instance of the XML parser in Mozilla browsers, use the following code:
如你希望在Mozilla浏览器中创建一个XML解析器,需要使用下述代码:

JavaScript 代码:

var xmlDoc=document.implementation.createDocument("ns","root",null);

The first parameter, ns, defines the namespace used for the XML document. The second parameter, root, is the XML root element in the XML file. The third parameter, null, is always null because it is not implemented yet.
第一个参数ns定义了用于XML文档的命名空间;第二个参数 root 是XML文件的XML根元素,因为第三个参数null是空值,所以它将不被执行。

The following code fragment loads an existing XML document ("note.xml") into Mozillas' XML parser:
下述代码片断在Mozillas的XML解析器中加载了一个现有的XML文件("note.xml") :

var xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");

The first line of the script above creates an instance of the XML parser. The second line tells the parser to load an XML document called "note.xml".
上述第一行脚本程序创建了一个XML解析器案例;第二行则指示解析器加载一份名为"note.xml"的XML文档。


Parsing an XML File - A Cross browser Example
解析一份 XML 文件 - 跨浏览器案例

The following example is a cross browser example that loads an existing XML document ("note.xml") into the XML parser:
下述案例是一个跨浏览器案例,它将现有XML文件("note.xml")加载到XML解析器:

<html>
<head>
<script type="text/javascript">

var xmlDoc;
function loadXML()
{
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
getmessage();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=getmessage;
}
else
{
alert('Your browser cannot handle this script');
}
}
function getmessage()
{
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
}

</script>
</head>
<body onload="loadXML()">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />

<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>

</p>
</body>
</html>

Output:
输出结果:

RuanChen Internal Note

To: Tove
From: Jani
Message: Don't forget me this weekend!

 


Important Note
重要节点

To extract the text (Jani) from an XML element like: <from>Jani</from>, the correct syntax is:
从XML元素中截取文本(Jani),(如:<from>Jani</from>),正确语法如下:

getElementsByTagName("from")[0].childNodes[0].nodeValue

IMPORTANT: getElementsByTagName returns an array of nodes. The array contains all elements with the specified name within the XML document. In this case there is only one "from" element, but you still have to specify the array index ( [0] ).
重点:getElementsByTagName会返回一组节点。这个数组包含了所有在XML文档中指定名称的元素。在上述案例中,虽然仅包含了一个“from”元素,但是,你仍然必须指定数组索引( [0] )。


Parsing an XML String - A Cross browser Example
解析XML字符串 - 跨浏览器案例

The following code is a cross-browser example on how to load and parse an XML string:
下面是一个跨浏览器的案例,它说明了关于加载和解析XML字符串的方法:

<html>
<body>
<script type="text/javascript">
var text="<note>";
text=text+"<to>Tove</to>";
text=text+"<from>Jani</from>";
text=text+"<heading>Reminder</heading>";
text=text+"<body>Don't forget me this weekend!</body>";
text=text+"</note>";
// code for IE
if (window.ActiveXObject)
{
var doc=new ActiveXObject("Microsoft.XMLDOM");
doc.async="false";
doc.loadXML(text);
}
// code for Mozilla, Firefox, Opera, etc.
else
{
var parser=new DOMParser();
var doc=parser.parseFromString(text,"text/xml");
}
// documentElement always represents the root node
var x=doc.documentElement;
document.write("Text of first child element: ");
document.write(x.childNodes[0].childNodes[0].nodeValue);
document.write("<br />");
document.write("Text of second child element: ");
document.write(x.childNodes[1].childNodes[0].nodeValue);

</script>
</body>
</html>

Output:
输出结果:

Text of first child element: Tove
Text of second child element: Jani

Note: Internet Explorer uses the loadXML() method to parse an XML string, while Mozilla browsers uses the DOMParser object.
注意:IE是通过 loadXML() 方法来解析XML字符串的;Mozilla浏览器则是使用 DOMParser 对象来解析的。

评论 (0) All

登陆 | 还没注册?