2 首先,加載這個xml文件,js中加載xml文件,是通過XMLDOM來進行的.
// 加載xml文檔
loadXML = function(xmlFile)
{
var xmlDoc;
if(window.ActiveXObject)
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;//是否異步加載xml文件(如果為ture,程序不論xml文件是否全部載入就開始運行下面程序,所以如果接下來就操作xml文件可能出錯)
xmlDoc.load(xmlFile);
}
else if (document.implementation&&document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument('', '', null);
xmlDoc.load(xmlFile);
}
else
{
return null;
}
return xmlDoc;
}
xml文件對象出來了, 接下去要對這個文檔進行操作了
比如說,需要得到節點Login/Weapon/W的第壹個節點的屬性,那麽可以如下進行:
// 首先對xml對象進行判斷
checkXMLDocObj = function(xmlFile)
{
var xmlDoc = loadXML(xmlFile);
if(xmlDoc==null)
{
alert('您的瀏覽器不支持xml文件讀取,於是本頁面禁止您的操作,推薦使用IE5.0以上可以解決此問題!');
window.location.href='/Index.aspx';
}
return xmlDoc;
}
// 然後開始獲取需要的Login/Weapon/W的第壹個節點的屬性值
var xmlDoc = checkXMLDocObj('/EBS/XML/Login.xml');
var v = xmlDoc.getElementsByTagName('Login/Weapon/W')[0].childNodes.getAttribute('Text')