var moz = (typeof document.implementation != 'undefined')
&&(typeofdocument.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
var orderDoc;
function importXML() //初始化Xml文档对象
{
if (moz)
{
orderDoc = document.implementation.createDocument("", "doc", null);//创建FIREFOX下XML文档对象
orderDoc.async = false;
}
else if (ie)
{
orderDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
orderDoc.async = false;
while( orderDoc.readyState != 4) {};//创建IE下XML文档对象
}
}
function getNode(doc, xpath) //ie中获取Xml节点内容 doc为Xml文档对象
{
var retval = "";
var value = doc.selectSingleNode(xpath);
if (value) retval = value.text;
return retval;
}
function LoadXmlInfo()
{
var path="a.xml";
orderDoc.load(path);
var info="";
if(ie)
{
for(var i=startindex;i>endindex;i--)
{
info=getNode(orderDoc,"/root/item["+i+"]/info");
//.....code;
}
}
if(moz)
{
var items=orderDoc.getElementsByTagName("item");
for(var i=startindex;i>endindex;i--)
{
info=items[i].getElementsByTagName("info")[0].childNodes[0].nodeValue;//
这段是重点 偶可是试过好几回的哦 当时很是郁闷呢!
//......code;
}
}
}
方法调用:先importXML(); 然后 LoadXmlInfo();
Xml文档内容:
内容