LOADXML.JS:
function importXML()
{
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = create;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) create()
};
}
else
{
alert('Your browser can/'t handle this script');
return;
}
xmlDoc.load("FileStruct1.xml");
}
function create(){
var root = xmlDoc.getElementsByTagName("ROOT");
for(var i = 0 ; i < root[0].childNodes.length; i++){
if(root[0].childNodes[i]){
getAlert(root[0].childNodes[i]);
}
}
}
function getAlert(e){
if(e.nodeType == 1){
alert(e.getAttribute("name"));
if(e.hasChildNodes()){
for(var i =0 ; i < e.childNodes.length; i++){
getAlert(e.childNodes[i]);
}
}
}
}
TREE.XML
<?xml version="1.0" ?>
<ROOT>
<branch id="a" name="A FILE">
<leaf id="a.a" name="B FILE"/>
<branch id="a.b" name="F FILE">
<leaf id="a.b.a" name="H FILE"/>
</branch>
<leaf id="a.c" name="C FILE"/>
</branch>
<leaf id="d" name="D FILE"/>
<leaf id="e" name="E FILE"/>
</ROOT>
这篇文章简单的举了个javascript解析xml的案例,在js文档可扩充的东西还有很多,从这个小demo中可以看到js对xml的解析还是比较方便的。