JQuery has powerful data parsing capabilities. This article describes how to parse the data of HTML, JSON, and XML files.
1. HTML
Sometimes, an HTML clip is saved in an HTML file. On the other homepage, the HTML file is read directly, and the HTML code snippet in the parsing is integrated into the home page.
Fragment.html file with the following content:
The Code is as follows:
Hello Jquery
On the homepage
Parse code in Test.html
The Code is as follows:
$ ("# A1"). click (function (){
$ ("# P2" pai.load('fragment.html ');
Return false;
}); [Code]
2. JSON
The JSON file is test. json with the following content:
[Code]
[{"Name": "jim", "age": "20" },{ "name": "lily", "age": "18", "holobby ": ["swim", "movie"]}]
On the homepage
Parse code in Test.html
The Code is as follows:
$ ("# A2"). click (function (){
$. GetJSON ('test. json', function (data ){
Var html ='
';$. Each (data, function (entryIndex, entry ){Html + ='
'+ Entry. name +' |
'+ Entry. age +' | ';If (entry. holobby ){Html + ='
'; $. Each (entry. holobby, function (lineindex, line ){ Html + = line + ","; }); Html + =' | ';}Html + ='
';});Html + ='
';
$ ("# P2" ).html (html );
Return false;
});
});
3. XML
The XML file is test. xml and its content is:
The Code is as follows:
Extjs
Zhang San
88
Sharp jQuery
Li Si
99
Introduction to flex
Wang Wu
108
Java programming ideas
Qian Qi
128
On the homepage
Parse code in Test.html
The Code is as follows:
$ ("# A3"). click (function (){
$. Get ('test. xml', function (data ){
Var s = "";
$ (Data). find ('book'). each (function (I ){
Var id = $ (this). attr ('id ');
Var name = $ (this). children ('name'). text ();
Var author = $ (this). children ('author'). text ();
Var price = $ (this). children ('price'). text ();
S + = id + "" + name + "" + author + "" + price +"
";
});
('{P2'{.html (s );
});
});
The original code of test.html is
The Code is as follows: