Jquery 读取解析xml文件并解析
Menus.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<root>
<Menuid="00"Parent="0"Depth="0"Children="4"Sequence="1"name="位置服务"DisableFlag="1100"Url=""/>
<Menuid="01"Parent="0"Depth="0"Children="0"Sequence="2"name="调度管理"DisableFlag="1100"Url=""/>
<Menuid="02"Parent="0"Depth="0"Children="0"Sequence="3"name="设备控制"DisableFlag="1100"Url=""/>
<Menuid="03"Parent="0"Depth="0"Children="0"Sequence="4"name="报警管理"DisableFlag="1100"Url=""/>
<Menuid="04"Parent="0"Depth="0"Children="0"Sequence="5"name="参数设置"DisableFlag="1100"Url=""/>
<Menuid="05"Parent="0"Depth="0"Children="0"Sequence="6"name="报表中心"DisableFlag="1100"Url=""/>
<Menuid="06"Parent="0"Depth="0"Children="0"Sequence="7"name="设备管理"DisableFlag="1100"Url=""/>
<Menuid="07"Parent="0"Depth="0"Children="0"Sequence="8"name="用户管理"DisableFlag="1100"Url=""/>
<Menu id="08" Parent="0" Depth="0" Children="0" Sequence="9" name="系统管理" DisableFlag="1100" Url=""/><Menu id="0001" Parent="00" Depth="1" Children="0" Sequence="1" name="定位服务" DisableFlag="1100" Url="1.html"/>
<Menuid="0002"Parent="00"Depth="1"Children="0"Sequence="2"name="历史轨迹"DisableFlag="1100"Url="2.html"/>
<Menuid="0003"Parent="00"Depth="1"Children="0"Sequence="3"name="我的地图"DisableFlag="1100"Url="#"/>
<Menu id="0004" Parent="00" Depth="1" Children="0" Sequence="4" name="请求服务" DisableFlag="1100" Url="#"/><Menu id="0101" Parent="01" Depth="1" Children="0" Sequence="1" name="调度管理1" DisableFlag="1100" Url="#"/>
<Menuid="0102"Parent="01"Depth="1"Children="0"Sequence="2"name="调度管理2"DisableFlag="1100"Url="#"/>
<Menuid="0103"Parent="01"Depth="1"Children="0"Sequence="3"name="调度管理3"DisableFlag="1100"Url="#"/>
<Menuid="0104"Parent="01"Depth="1"Children="0"Sequence="4"name="调度管理4"DisableFlag="1100"Url="#"/>
</root>JS代码
//设置延迟2毫秒来加载主菜单
setTimeout("showMenuList()","200");
functionshowMenuList()
{
varmainMenuUrl="Common\\Menus.xml";
varmenuItem="";
$.ajax({
url:mainMenuUrl,
type:"POST",
dataType:($.browser.msie)?"text":"xml",
success:function(data){
varxml;
if(typeofdata=="string"){
xml=newActiveXObject("Microsoft.XMLDOM");
xml.async=false;
xml.loadXML(data);
}else{
xml=data;
}
varindex=0;
$(xml).find("Menu").each(function(i){
varID=$(this).attr('ID');
varName=$(this).attr("Name");
varParent=$(this).attr("Parent");
//选择所有父节点为0的元素作为第一层菜单元素,菜单默认选择的是第一个按钮,设置第一个按钮的class=active
if(Parent=='0')
{
if(index==0)
{
menuItem+='<divclass="active"id="'+ID+'"><ahref="#"onclick="selectMainMenu(\''+ID+'\')">'+Name+'</a></div>';
}
else
{
menuItem+='<divid="'+ID+'"><ahref="#"onclick="selectMainMenu(\''+ID+'\')">'+Name+'</a></div>';
}
}
index++;
});
$("#mainMenu").html(menuItem);
},
error:function(XMLHttpRequest,textStatus,errorThrown){
$("#mainMenu").html("加载异常,请联系客服人员。");
}
});
}