+ -
当前位置:首页 → 问答吧 → javascripy读取xml节点内容

javascripy读取xml节点内容

时间:2007-12-24

来源:互联网

有个 a.xml 文件 怎么用JS取得 ( 第一个节点 )里面的 (td2) 内容 ,和(第二个节点) 里面的t(d3)内容。本人刚学xml只会调用只有一个根节点的内容。有几个子节点的内容就不会了。求大侠指教一下。本菜鸟感激不尽。
内容如下
<ta>

<第一个节点>
<td1>1</td1>
<td2>2</td2>
</第一个节点>

<第二个节点>
<td3>3</td3>
<td4>4</td4>
</第二个节点>

</ta>


javascript 应该怎么写?

作者: terrymayx   发布时间: 2007-12-24


<html> <head> <script type=text/Jscript defer> function initXml() { var mx=new ActiveXObject("Microsoft.XMLDOM"); mx.async = false;//,是否异步通信,不加也没事 mx.loadXML(product_list.innerHTML);//如果访问xml文件请用load(); for(var i=0;i<mx.selectNodes("/minfo/product").length;i++)//mx.selectNodes("/minfo/product")就是product的结点列表 { var ltr=mtable.insertRow(); var id=mx.selectSingleNode("/minfo/product["+i+"]/productid");//获得第i个结点的productid var name=mx.selectSingleNode("/minfo/product["+i+"]/productname");//获得第i个结点的productname var price=mx.selectSingleNode("/minfo/product["+i+"]/unitprice");//获得第i个结点的unitprice var stock=mx.selectSingleNode("/minfo/product["+i+"]/unitsinstock");//获得第i个结点的unitsinstock ltr.insertCell().innerText=id.text;//插入一行文本为id的text值,也就是productid里的值 ltr.insertCell().innerText=name.text; ltr.insertCell().innerText=price.text; ltr.insertCell().innerText=stock.text; } } initXml(); </script> </head> <body bgcolor="#ffffff"> <!--xml文件开始--> <xml id='product_list'> <minfo> <product> <productid>1</productid> <productname>Chai</productname> <unitprice>18.0000</unitprice> <unitsinstock>39</unitsinstock> </product> <product> <productid>2</productid> <productname>Chang</productname> <unitprice>19.0000</unitprice> <unitsinstock>17</unitsinstock> </product> <product> <productid>3</productid> <productname>Aniseed Syrup</productname> <unitprice>10.0000</unitprice> <unitsinstock>13</unitsinstock> </product> <product> <productid>4</productid> <productname>Chef Anton's Cajun Seasoning</productname> <unitprice>22.0000</unitprice> <unitsinstock>53</unitsinstock> </product> <product> <productid>5</productid> <productname>Chef Anton's Gumbo Mix</productname> <unitprice>21.3500</unitprice> <unitsinstock>0</unitsinstock> </product> <product> <productid>6</productid> <productname>Grandma's Boysenberry Spread</productname> <unitprice>25.0000</unitprice> <unitsinstock>120</unitsinstock> </product> <product> <productid>7</productid> <productname>Uncle Bob's Organic Dried Pears</productname> <unitprice>30.0000</unitprice> <unitsinstock>15</unitsinstock> </product> </minfo> </xml> <!--xml文件结束--> <table border="1" cellpadding=3 cellspacing=0 id="mtable" width="50%"> <tr> <td width="40%">产品名称</td> <td width="20%">单价</td> <td width="20%">数量</td> <td width="20%">操作</td> </tr> </table> </body> </html>
   提示:您可以先修改部分代码再运行

作者: sixdo   发布时间: 2008-01-06

很好的例子,谢谢sixdo 了,
再问一下,.net中ActiveXObject("Microsoft.XMLDOM")是对应的什么类呀
我可以直接变换类后直接使用么?

作者: straw   发布时间: 2008-02-22

System.Xml.XmlDocument

作者: jscode   发布时间: 2008-03-28