Function Description: Use jquery to parse XML files with defined content
1. xml file: menu. xml
Code
<? XML version = "1.0" encoding = "gb2312" ?>
< Menus >
< Menu >
< ID > 1 </ ID >
< Name > System </ Name >
< Parentid > 0 </ Parentid >
< Target > Mainframe </ Target >
< URL > </ URL >
</ Menu >
< Menu >
< ID > 2 </ ID >
< Name > Company </ Name >
< Parentid > 1 </ Parentid >
< Target > Mainframe </ Target >
< URL > </ URL >
</ Menu >
</Menus>
2. parsing XML files using custom JS files: Index. js
Code
$ (Document). Ready ( Function (){
$. Ajax ({
URL: " XML/menu. xml " ,
Type: " Post " ,
Datatype: " XML " ,
Success: Function (Data ){
$ (Data). Find ( " Menu " ). Each ( Function (){
Alert ( " Success " );
Alert ($ ( This ). Text ());
});
},
Error: Function (Data ){
Alert ( " Fail: " );
}
});
});
3.The newly created index.html file is imported into the jquery.js?xmlfile and index.js file. "fail" is always displayed when you run index.html in ie( )), but "success" is returned in Firefox ".
I searched a lot of information on the Internet.ArticleBytes. The restriction in IE cannot parse the XML file correctly. It parses a Text object (which is explained in more detail in the document I referenced). Therefore, this can be solved by adding a judgment to this object.
CorrectCodeIndex. js
Code
$ (Document). Ready ( Function (){
$. Ajax ({
URL: " XML/menu. xml " ,
Type: " Post " ,
Datatype :( $. browser. MSIE) ? " Text " : " XML " ,
Success: Function (Data ){
VaR XML;
If ( Typeof Data = " String " ){
XML = New Activexobject ( " Microsoft. xmldom " );
XML. async = False ;
XML. loadxml (data );
} Else {
XML = Data;
}
$ (XML). Find ( " Menu " ). Each ( Function (){
Alert ( " Success " );
Alert ($ ( This ). Text ());
});
},
Error: Function (Data ){
Alert ( " Fail: " );
}
});
});