js遍历xml节点树

 
  
< html >
< body >
< script type = " text/javascript " >
var xmlDoc;
// code for IE
if (window.ActiveXObject)
{
xmlDoc
= new ActiveXObject( " Microsoft.XMLDOM " );
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation.createDocument)
{
xmlDoc
= document.implementation.createDocument( "" , "" , null );
}
else
{
alert(
' Your browser cannot handle this script ' );
}
xmlDoc.async
= false ;
xmlDoc.load(
" /example/xmle/note.xml " );

var x = xmlDoc.documentElement.childNodes;

for ( var i = 0 ;i < x.length;i ++ )
{
if (x[i].nodeType == 1 )
{
// Process only element (nodeType 1) nodes
document.write(x[i].nodeName + " : " );
document.write(x[i].childNodes[
0 ].nodeValue);
document.write(
" <br /> " );
}
}
< / script>
< / body>
< / html>

xml文件

 
  
<? xml version="1.0" encoding="ISO-8859-1" ?>
-
<!-- Copyright w3school.com.cn
-->
-
< note >
< to > George </ to >
< from > John </ from >
< heading > Reminder </ heading >
< body > Don't forget the meeting! </ body >
</ note >

 

结果:

to: George
from: John
heading: Reminder
body: Don't forget the meeting!

转载于:https://www.cnblogs.com/lovening/archive/2010/09/23/1833277.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值