js读取xml 浏览器兼容
RT 搞不动啊
网上搜的一用 都是不行的
html:
<html>
<body>
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","photo.xml",false);/*IE这个就通不过*/
xmlhttp.send();/*Chrome 和 Opera 卡这里就完了 - -|||*/
xmlDoc=xmlhttp.responseXML;
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("item");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("path")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("info")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>
</body>
</html>
<?xml version="1.0" encoding="gb2312"?>
<root>
<item>
<photo1>
<path>/pic/pic1.jpg</path>
<info>一</info>
</photo1>
<photo2>
<path>/pic/pic2.jpg</path>
<info>二</info>
</photo2>
<info>三</info>
</item>
<item>
<photo1>
<path>/pic/pic1.jpg</path>
<info>一</info>
</photo1>
<photo2>
<path>/pic/pic2.jpg</path>
<info>二</info>
</photo2>
<info>三</info>
</item>
</root>
/**
* @param xmlFile xml文件路径
* @return 返回 XML DOM 对象
*/
function getXMLDoc(xmlFile) {
var xmlDoc;
try{
if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
else if (document.implementation) {
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
}catch(e){
xmlDoc = undefined;
}
return xmlDoc;
}