兼容在IE和Firefox浏览器下操作XML(英)

本文摘自 http://www.w3schools.com,虽是英文版,但其脚本用法相信大家还是看得懂的,希望对大家编写基于xml的网页有所帮助。

To read and update - create and manipulate - an XML document, you will need an XML parser.


Microsoft's XML Parser

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.

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.

The following table lists the most commonly used node types supported by Microsoft's XML parser:

Node TypeExample
Processing instruction<?xml version="1.0"?>
Element<drink type="beer">Carlsberg</drink>
Attribute type="beer"
TextCarlsberg

MSXML Parser 2.5 is the XML parser that is shipped with Windows 2000 and IE 5.5.

MSXML Parser 3.0 is the XML parser that is shipped with IE 6.0 and Windows XP.

The MSXML 3.0 parser features:

  • JavaScript, VBScript, Perl, VB, Java, C++, etc. support
  • Complete XML support
  • Full DOM and Namespace support
  • DTD and validation
  • Complete XSLT and XPath support
  • SAX2 support
  • Server-safe HTTP

To create an instance of Microsoft's XML parser with JavaScript, use the following code:

To create an instance of Microsoft's XML parser with VBScript, use the following code:

set xmlDoc=CreateObject("Microsoft.XMLDOM")

To create an instance of Microsoft's XML parser in an ASP page (using VBScript), use the following code:

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

The following code loads an existing XML document ("note.xml") into Microsoft's XML parser:

<script type="text/javascript">

...
...
...
</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 Parser in Mozilla Browsers

Plain XML documents are displayed in a tree-like structure in Mozilla (just like IE).

Mozilla also supports parsing of XML data using JavaScript. The parsed data can be displayed as HTML.

To create an instance of the XML parser with JavaScript in Mozilla browsers, use the following code:

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.

The following code loads an existing XML document ("note.xml") into Mozillas' XML parser:

<script type="text/javascript">

...
...
...
</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".


Loading an XML File - A Cross browser Example

The following example is a cross browser example that loads an existing XML document ("note.xml") into the XML parser:

<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.("note.xml");
  getmessage()
  }
// code for Mozilla, etc.
else if (document.implementation &&
document.implementation.createDocument)
  {
  xmlDoc= document.implementation.createDocument("","",null);
  xmlDoc.("note.xml");
  xmlDoc.οnlοad=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 οnlοad="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


Loading XML Text Into the Parser

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.

The following code loads a text string into Microsoft's XML parser:

<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.(txt)
...
...
...
</script>

If you have Internet Explorer, you can try it yourself.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值