javascript加载xml 并解析各节点的值(实现方法)
实例如下:
var xmlDoc = null; function LoadXml(xmlPath) { try { if (window.ActiveXObject) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); } } catch (e) { try { xmlDoc = document.implementation.createDocument("", "", null); } catch (e) { alert(e.Message); } } try{ xmlDoc.async=false; xmlDoc.load(xmlPath); return (xmlDoc); } catch (e) { alert(e.Message); } } //根据节点名称获取属性的值 function GetNodes(nodeName, attrName) { var xmldata = LoadXml("test.xml"); //获取xml第一个节点的值 //var booksName = xmldata.getElementsByTagName("bookName")[0].childNodes[0].nodeValue; // //获取所有xml下面所有节点的值 // var docElement=xmldata.getElementsByTagName("root"); // for(var j=0;j<docElement.length;j++){ // //获取作者 // var Author=docElement[i].getElementsByTagName("Author")[0].firstChild.nodeValue; // //获取时间 // var time=docElement[i].getElementsByTagName("time")[0].firstChild.nodeValue; // //获取作者的联系方式 // var phone=docElement[i].getElementsByTagName("phone")[0].firstChild.nodeValue; // } //FF var nodes = xmldata.selectNodes("//" + nodeName); for (var i = 0; i < nodes.length; i++) { //获取属性名称节点的值 alert(nodes[i].getAttribute(attrName); } }
FF下获取 xml属性 节点的值
var cNodes=xmlDoc.getElementsByTagName("book"); for (var j = 0; j < cNodes.length; i++) { var bookID = xmlDoc.getElementsByTagName("book")[j].getAttribute("bookID"); //获取book节点下面的bookTitle节点的值 var bookTitile = xmlDoc.getElementsByTagName("book")[j].childNodes[0].nodeValue;
相关推荐
wikiwater 2020-10-27
jczwilliam 2020-08-16
littleFatty 2020-08-16
Aveiox 2020-06-23
阿斌Elements 2020-06-11
zrtlin 2020-11-09
xuebingnan 2020-11-05
heheeheh 2020-10-19
Crazyshark 2020-09-15
softwear 2020-08-21
ZGCdemo 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04
xiaofanguan 2020-06-25