<html>
<head>
<title>xml</title>
<style type="text/css">
.colors{background-color: red;}
</style>
<script type="text/javascript">
function loadXmlFile(xmlFile)//xmlFile 鏄痻ml鏂囦欢鐨勫湴鍧�
{
var xmlDom = null;
if (window.ActiveXObject)//IE娴忚鍣ㄤ腑璇诲彇xml鏂囦欢
{
xmlDom = new ActiveXObject("Microsoft.XMLDOM");
xmlDom.async="false";
xmlDom.load(xmlFile);
}
else if(document.implementation && document.implementation.createDocument)
{
//Firefox锛孋hrome 娴忚鍣ㄤ腑璇诲彇xml鏂囦欢 ,Chrome 闇�寮�湇鍔″櫒鎵嶈兘璁块棶
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", xmlFile, false);
xmlhttp.send(null);
xmlDom = xmlhttp.responseXML;
}
else
{
xmlDom = null;
}
return xmlDom;
};
//濂戒簡璇诲彇鍑烘潵浜唜ml 鎴戜滑灏辫鑾峰彇鐨勪粬鐨勬暟鎹簡
function gerWords(str)
{
//鐢熸垚鍏抽敭璇嶉摼鎺�
var xdoc =loadXmlFile("data3.xml")
var elements=xdoc.getElementsByTagName("Company");
alert(elements.length);
//var list=xdoc.selectNodes("//keys")//鍙兘鍦↖e娴忚鍣ㄤ腑鑾峰彇
/*var list=xdoc.getElementsByTagName("keys");//璇诲彇鎵�湁鑺傜偣鍊�
var table = document.createElement('table');
table.createCaption().innerHTML = 'xml鏁版嵁琛�;
table.border = 1;
table.className = 'colors';
var thead = table.createTHead();
var tr = thead.insertRow(0);
var td1 = tr.insertCell(tr.cells.length);
td1.innerHTML = "keys";
var td2 = tr.insertCell(tr.cells.length);
td2.innerHTML = "name";
var tbody = document.createElement('tbody');
table.appendChild(tbody);
for (var i = 0; i < list.length; i++)
{
var trx = tbody.insertRow(tbody.rows.length);
var tdx1 = trx.insertCell(trx.cells.length);
var tdx2 = trx.insertCell(trx.cells.length);
tdx1.innerHTML = "keys";
tdx2.innerHTML = list[i].getAttribute("name");
}
document.getElementsByTagName('body')[0].appendChild(table);*/
};
window.οnlοad=function(){
gerWords();
};
</script>
</head>
<body>
</body>
</html>
js解析本地xml文件
最新推荐文章于 2024-07-27 20:15:58 发布