js 读 xml 非ie 可以支持 chrome 浏览器 与 android webView

//read.xml内容

<?xml version="1.0" encoding="utf-8" ?>

<contents>

<itemcount>18</itemcount>

<item>

<title>美女</title>

<content />

<filetype>jpg,gif</filetype>

<filecount>37</filecount>

</item>

</contents>

//网页内容

<html>

<head>

<script>

var request_success = false;

var dataArray = new Array();

var itemArray = new Array();

function OnLoad() {

var request = new XMLHttpRequest();

var my_div = document.getElementById("my_div");

var my_div_1 = document.getElementById("my_div_1");

request.open("GET", "../test5/xml/read.xml", false);

request.send("");

if (request.readyState == 4 && request.status == 200) {

request_success = true;

String

str = "";

var item1 = request.responseXML.getElementsByTagName("item");

alert(item1.length);

var dataA = Array.prototype.slice.call(item1);

for ( var i = 0; i < dataA.length; i++) {

var oneItem = dataA[i].childNodes;

var oneItemLen = oneItem.length;

itemArray = new Array();

for ( var j = 1; j < oneItemLen; j++) {

itemArray.push(oneItem[j].textContent);

j++;

}

dataArray.push(itemArray);

}

for ( var k1 = 0; k1 < dataArray.length; k1++) {

var oneItemData = dataArray[k1];

str += k1 + " ";

for ( var k2 = 0; k2 < oneItemData.length; k2++) {

str += oneItemData[k2] + " " + k2;

}

str += "<br/>";

}

alert(dataArray.length);

var src1 = dataArray[0][1];

my_div_1.innerHTML = src1;

my_div.innerHTML = "<img src='"+src1+"'/>";

} 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>

<div id="my_div_1"></div>

</body>

</html>

posted @ 2011-09-22 14:20  镇水古月  阅读(338)  评论(0编辑  收藏  举报