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>