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

XML
XML DHTML行为
XML 相关技术
XML 编辑器
XML 摘要
XML 实例
XML字符编码
xml 文档树
IE和火狐读取XML方法比较

XML 解析器


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

To read and update - create and manipulate - an XML document, you will need an XML parser.
如果你希望阅读、更新、创建以及操作一份XML文档,你就需要用到一个XML解析器。


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形式。

The following table lists the most commonly used node types supported by Microsoft's XML parser:
下面的表格列举了微软的XML解析器所支持的当前最常用的节点类型:

Node Type
Node 类型
Example
举例
Processing instruction [处理指令] <?xml version="1.0"?>
Element [元素] <drink type="beer">Carlsberg</drink>
Attribute [属性] type="beer"
Text [文本] Carlsberg

MSXML Parser 2.5 is the XML parser that is shipped with Windows 2000 and IE 5.5.
MSXML Parser 2.5 是随Windows 2000 和 IE 5.5 的安装而自动安装的XML解析器。

MSXML Parser 3.0 is the XML parser that is shipped with IE 6.0 and Windows XP.
MSXML Parser 3.0 是随IE 6.0 和 Windows XP的安装而自动安装的XML解析器。

The MSXML 3.0 parser features:
MSXML 3.0解析器的具体特征:

  • JavaScript, VBScript, Perl, VB, Java, C++, etc. support
    支持JavaScript、VBScript、Perl、VB、Java、C++等语言。
  • Complete XML support
    完全支持XML。
  • Full DOM and Namespace support
    完全支持DOM 和 命名空间
  • DTD and validation support
    支持DTD 和有效性验证
  • Complete XSLT and XPath support
    完全支持XSLT 和 XPath
  • SAX2 support
    支持SAX2
  • Server-safe HTTP support
    支持HTTP服务器安全协议

To create an instance of Microsoft's XML parser with JavaScript, use the following code:
用 JavaScript 语言编一个微软XML解析器的例子,可以使用下面的代码:

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

To create an instance of Microsoft's XML parser with VBScript, use the following code:
用 VBScript 编写一个微软XML解析器的例子,可以使用下面的代码:

set xmlDoc=CreateObject("Microsoft.XMLDOM")

To create an instance of Microsoft's XML parser in an ASP page (using VBScript), use the following code:
通过使用VBScript 在ASP页面中创建一个微软XML解析器,可以使用下面的代码:

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

The following code loads an existing XML document ("note.xml") into Microsoft's XML parser:
下面的代码把当前的XML文档("note.xml")加载到现存的微软XML解析器中:

<script type="text/javascript">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
...
...
...
</script>

The first line of the script above creates an instance of the Microsoft XML parser. The third line tells the parser to load an XML document called "note.xml". 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.
上述脚本语句的第一行创建了一个微软XML解析器的实例;第三行指示解析器加载一份名为"note.xml" 的XML文档;第二行关闭了同步加载功能,确保解析器在文档彻底加载完毕之前不会继续执行脚本语句。


XML Parser in Mozilla Browsers
Mozilla浏览器上的XML解析器

Plain XML documents are displayed in a tree-like structure in Mozilla (just like IE).
在Mozilla浏览器中,普通的XML文档将以树状结构被显示出来(着类似于IE浏览器)。

Mozilla also supports parsing of XML data using JavaScript. The parsed data can be displayed as HTML.
Mozilla也支持使用JavaScript 解析XML数据。被解析的数据可以通过HTML的形式显示出来。

To create an instance of the XML parser with JavaScript in Mozilla browsers, use the following code:
在Mozilla浏览器上用JavaScript 创建一个微软XML解析器的例子,可以使用下面的代码:

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 loads an existing XML document ("note.xml") into Mozillas' XML parser:
下述代码将当前的一份XML文档("note.xml")加载到Mozilla的XML解析器中:

<script type="text/javascript">

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

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文档。


Loading 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()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
getmessage()
}
// code for Mozilla, 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].firstChild.nodeValue
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].firstChild.nodeValue
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue
}

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

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

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

Try it yourself
自己尝试一下

There are more examples of this in our XML DOM tutorial.
察看更多的案例清访问我们的 XML DOM 教程


Loading XML Text Into the Parser
把XML 文本载入解析器

Internet Explorer supports two ways of loading XML into a document object: the load() method and the loadXML() method. The load() method loads an XML file and the loadXML() method loads a text string that contains XML code.
IE浏览器支持两种方法将XML加载到文本对象中:“load()” 方法和“loadXML()”方法。“load()”方法载入一份XML文件,“loadXML()”方法载入含有XML编码的文本字符串。

The following code loads a text string into Microsoft's XML parser:
下述代码将一段文本字符串加载到微软的XML解析器中:

<script type="text/javascript">
var txt="<note>"
txt=txt+"<to>Tove</to><from>Jani</from>"
txt=txt+"<heading>Reminder</heading>"
txt=txt+"<body>Don't forget me this weekend!</body>"

txt=txt+"</note>"
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(txt)
...
...
...
</script>

If you have Internet Explorer, you can try it yourself.
如果你有IE,你可以 自己尝试一下

评论 (0) All

登陆 | 还没注册?