<html>
<head><title></title>
<script>
function fun_loadXml(filename)
{
var xmlDom=new ActiveXObject("Microsoft.XMLDOM");
xmlDom.async=false;
try{
xmlDom.load(filename);
if(xmlDom.parseError!=0)
{
alert(xmlDom.parseError.reason);
return null;
}
}catch(e)
{
}
return xmlDom;
}
function fun_load()
{
var xmlDom=fun_loadXml("books.xml");
if(xmlDom!=null)
{
/*alert(xmlDom.xml);
var namelist=xmlDom.getElementsByTagName("name");
for(var i=0;i<namelist.length;i++)
{
var eleName=namelist[i];
var txtNode=eleName.firstChild;
alert(eleName.nodeName+":"+txtNode.nodeValue);
}*/
var root=xmlDom.documentElement;
alert(root.childNodes.length);
var booklst=xmlDom.getElementsByTagName("book");
for(var i=0;i<booklst.length;i++)
{
var bookNode=booklst[i];
var bookAtt=bookNode.attributes[0];
alert(bookAtt.nodeName+":"+bookAtt.nodeValue);
var bookChilds=bookNode.childNodes;
for(var j=0;j<bookChilds.length;j++)
{
alert(bookChilds[j].nodeName+":"+bookChilds[j].firstChild.nodeValue);
}
}
}
}
</script>
</head>
<body onLoad="fun_load();">
<textarea cols="20" rows="30" name="xmlContent"></textarea>
</body>
</html>
JAVASCRIPT 解析XMLDOM
最新推荐文章于 2024-08-14 15:45:18 发布