data.xml
<?xml version="1.0" encoding="gb2312"?>
<employees>
<employee name="Billgates">
<job>Programmer</job>
<salary>32768</salary>
</employee>
<employee name="王涛">
<job>无业游民</job>
<salary>70000</salary>
</employee>
<employee name="Big 中华">
<job>哈尔滨CEO</job>
<salary>100000</salary>
</employee>
</employees>
<script language="javascript">
//类的构造,传入xml文档和需要处理的标签名称
function DataSet(xmlFile, tagLabel) {
var xmldoc;
if(window.ActiveXObject){
xmldoc=new ActiveXObject("Microsoft.XMLDOM");
}else if(document.implementation&&document.implementation.createDocument){
xmldoc=document.implementation.createDocument("","doc",null);
}
xmldoc.async = false;
xmldoc.load(xmlFile);
this.rootObj = xmldoc.getElementsByTagName(tagLabel)
this.getCount = getCount;
this.getData = getData;
this.getAttribute = getAttribute;
this.getNodeAttribute=getNodeAttribute;
}
function getCount(){
return this.rootObj.length
}
function getData(index, tagName){
if (index >= this.count) return "index overflow"
var node = this.rootObj[index]
var str = node.getElementsByTagName(tagName)[0].firstChild.data
return str
}
function getAttribute(index, tagName) {
if (index >= this.count) return "index overflow"
var node = this.rootObj[index]
var str = node.getAttribute(tagName)
return str
}
function getNodeAttribute(index,node,tagName){
if(index>=this.rootObj[index].childNodes.length)return "index overflow";
var str=this.rootObj[index].childNodes[node].getAttribute(tagName);
return str;
}
var set=new DataSet("data.xml","employee");
alert(set.getCount());
alert(set.getAttribute(2,"name"));
alert(set.getData(1,"job"));
</script>