DOM解析XML

To manipulate an XML document, you need an XML parser. The parser loads the document into your computer's memory and allows you to manipulate it using the DOM.

There are some differences between Microsoft's XML parser and the XML parser used in Mozilla browsers. The loadXMLDoc function is a cross browser script that will allow parsing in both Internet Explorer and Mozilla browsers.

The following JavaScript function is stored in the "loadxmldoc.js" file and loaded in the <head> of the examples:

function loadXMLDoc(dname) 

{

var xmlDoc;

// code for IE

if (window.ActiveXObject)

{

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

}

// code for Mozilla, Firefox, Opera, etc.

else if (document.implementation && document.implementation.createDocument)

{

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

}

else

{

alert('Your browser cannot handle this script');

}

xmlDoc.async=false;

xmlDoc.load(dname);

return(xmlDoc);

}





--------------------------------------------------------------------------------

The code above gets the XML document from the script (dname), and checks to see if what kind of browser is used. When the browser type is found, it creates the right parser type:



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 Microsoft XML parser is created with the following code:

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



--------------------------------------------------------------------------------

XML Parser in Mozilla, Firefox, and Opera

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.

The XML parser in Mozilla browsers is created with the following code:

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.


--------------------------------------------------------------------------------

Loading the File and Returning the XML Document

After the right parser is created the following code is run:

xmlDoc.async=false;

xmlDoc.load(dname);

return(xmlDoc);

=======分割线一=======
1。获取一个元素值
<script type="text/javascript">
xmlDoc=loadXMLDoc("/text/books.xml");
var x=xmlDoc.getElementsByTagName('title');
for (i=0;i<x.length;i++)

{

document.write(x[i].childNodes[0].nodeValue)

document.write("<br />")

}

</script>

2。获取一个属性值
TAG.getAttribute("Attribute_name")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值