<?xml version="1.0" encoding="gb2312"?> <PolicyList> <Policy id="A"> <Name>黄晓明</Name> <Add>58</Add> </Policy> <Policy name="B"> <Name>黄晓明</Name> <Add>59</Add> </Policy> </PolicyList> 假设你的xml文档名为policy.xml <html> <head><title></title><head> <body> <script type="text/javascript"> var xmlDoc try{ xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); document.write("/"); } catch(e) { try{ xmlDoc=document.implementation.createDocument("","",null); } catch(e) { alert("浏览器不支持!!"); } } xmlDoc.async=false; xmlDoc.load("policy.xml"); var x=xmlDoc.getElementsByTagName("Policy"); document.write("<br>()"); document.write(x.length); document.write("()<br>"); for(i=0;i<x.length;i++) { if(x[i].getAttribute("id")=='A') { name=x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue; add=x[i].getElementsByTagName("Add")[0].childNodes[0].nodeValue; break; } } document.write('name:'+name+' '+'add:'+add); </script> </body> </html> html 命名为policy.html 与policy.xml同一个目录下
javascript 操作xml
最新推荐文章于 2021-06-20 22:35:56 发布