js 读 xml 非ie浏览器 如:chrome
参考资料:http://www.w3school.com.cn/xmldom/dom_element.asp
//read2.xm内容如下
<?xml version="1.0" encoding="utf-8" ?>
<contents>
123213
<a>111</a>
</contents>
//html内容如下:
<html>
<head>
<script>
var request_success = false;
function OnLoad() {
var request = new XMLHttpRequest();
request.open("GET", "../test5/xml/read2.xml", false);
request.send("");
if (request.readyState == 4 && request.status == 200) {
request_success = true;
var x = request.responseXML.getElementsByTagName("contents");
alert(x.length);
var arr = Array.prototype.slice.call(x);
alert(arr[0].textContent);
my_div.innerHTML = "Got a successful response: "+ request.responseText;
} else {
my_div.innerHTML = "Request failed: ready state="
+ request.readyState + " status=" + request.status;
}
}
function DidSyncRequestSucceed() {
return request_success;
}
</script>
</head>
<body onload="OnLoad();">
This page sends a synchronous XMLHttpRequest.
<br>
<br>
<div id="my_div"></div>
</body>
</html>