JS读取xml
xml文件
<?xml version="1.0" encoding="utf-8"?> <root> <data id="1"> <name>ceun</name> <age>21</age> </data> <data id="2"> <name><![CDATA[张三]]></name> <age>22</age> </data> <data id="3"> <name>jake</name> <age>23</age> </data> <data id="4"> <name>hello</name> <age>20</age> </data> <data id="5"> <name>Paul</name> <age>25</age> </data> <Page> <Timer>1</Timer> </Page> </root>
js读取
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script> var xmlDoc = null, xmlhttp = null; function loadXML() { xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp"); if (xmlhttp == null) { alert("你的浏览器不支持 XMLHttpRequest"); return; } xmlhttp.open("GET", "../xml1.xml", true); xmlhttp.setRequestHeader("Content-Type", "text/xml"); xmlhttp.onreadystatechange = getmessage; xmlhttp.send(null); } function getmessage() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { xmlDoc = xmlhttp.responseXML.documentElement; if (xmlDoc == null) { alert("返回的数据不正确。"); return; } document.getElementById('p1').innerText = xmlDoc.getElementsByTagName('name')[0].childNodes[0].nodeValue; } } </script> </head> <body onload="loadXML()"> <p id="p1"></p> </body> </html>
参考:https://blog.csdn.net/ywb201314/article/details/49705965