Solution for XML file parsing by jquery in IE

Source: Internet
Author: User

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: " );
}
});
});

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.