本节内容:
获取XML某节点的子节点个数
 
1,xml文件 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>
2,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) 
{ // www.jb200.com
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>