a.xml:
複製代碼 代碼如下:<?xml version="1.0" encoding="gb2312"?>
<pnode>
<node xmlId="0" />
<node xmlId="1" />
<node xmlId="2" />
<node xmlId="3" />
<node xmlId="4" />
</pnode>
javascript: 複製代碼 代碼如下:<script language="javascript" type="text/javascript">
//載入xml文檔
function loadXML(xmlFile)
{
var xmlDoc;
if(window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}
else if (document.implementation&&document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.load(xmlFile);
}
else
return null;
return xmlDoc;
}
// 首先對xml對象進行判斷
function checkXMLDocObj(xmlFile)
{
var xmlDoc = loadXML(xmlFile);
if(xmlDoc==null)
{
alert('您的瀏覽器不支援xml檔案讀取,於是本頁面禁止您的操作,推薦使用IE5.0以上可以解決此問題!');
return false;
}
return xmlDoc;
}
var xmlDoc=checkXMLDocObj("a.xml");
//if(window.ActiveXObject) xmlDoc.removeChild(xmlDoc.childNodes[0]);
var topM=xmlDoc.getElementsByTagName("node1")[0].childNodes;
alert(topM.length);
</script>