jquery Reads XML documents and displays data examples

Source: Internet
Author: User

1. Note The $.ajax attribute datatype: ' XML '
2.Java out.println in Response.setcontenttype ("Text/xml"), also must be in XML format
3. Parsing an XML document is the same as parsing the DOM, you can use the Find (), children () function to parse and use each () method to traverse, in addition to the text () and the attr () method to get the node texts and properties.

1. Read XML documents directly

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Utf-8"?>
<books>
<book title= "Tibet Code" imageurl= "Images/tibet_code.jpg" >
<description>
Here is the overview (www.111cn.net)
</description>
</book>

<book title= "Cambridge IELTS 6" imageurl= "Images/ielts.jpg" >
<description>
Here is the overview (www.111cn.net)
</description>
</book>

<book title= "Professional asp.net" imageurl= "images/asp.jpg" >
<description>
Here is the overview (www.111cn.net)
</description>
</book>
</books>

jquery Code

The code is as follows Copy Code
$ (document). Ready (function ()


{


$.get (' XMLFile.xml ', function (d) {


$ (' body '). Append (' &lt;h1&gt; Saturn to recommend some books to you: &lt;/h1&gt; ');


$ (' body '). Append (' &lt;dl/&gt; ');





$ (d). Find ("book"). each (function () {


var $book = $ (this);


var title = $book. attr ("title");


var description = $book. Find (' description '). Text ();


var ImageUrl = $book. attr (' ImageUrl ');





var html = ' &lt;dt&gt; &lt;img class= ' bookimage "alt=" "src=" ' + ImageUrl + ' "mce_src=" ' + ImageUrl + ' "/&gt; &lt;/dt&gt ;';


html = ' &lt;dd&gt; &lt;span class= ' loadingpic ' alt= ' Loading '/&gt; ';


html + + ' &lt;p class= ' title ' &gt; ' + title + ' &lt;/p&gt; ';


html + + ' &lt;p&gt; ' + description + ' &lt;/p&gt; ';


HTML + = ' &lt;/dd&gt; ';





$ (' DL '). Append ($ (HTML));





$ ('. Loadingpic '). fadeout (2000);


});


});


});



2. Based on jquery Ajax examples

The code is as follows Copy Code

function Standardtaxrate ()


{


$.ajax ({


URL: "/resource/xml.php",


DataType: ' xml ',


Type: ' Get ',


timeout:2000,


Error:function (XML)


{


Alert ("Error loading XML file!") ");


},


Success:function (XML)


{


$ (XML). Find ("TaxRate"). each (function (i)


{


var oid = $ (this). attr ("id");


var lower = $ (this). Children ("lower"). Text ();


var upper = $ (this). Children ("upper"). Text ();


var rate = $ (this). Children ("rate"). Text ();


var buckle = $ (this). Children ("buckle"). Text ();


Subsequent operation ...


});


}


});


}

xml.php just return XML format data

The code is as follows Copy Code

&lt;?xml version= "1.0" encoding= "Utf-8"?&gt;


&lt;taxrates&gt;


&lt;taxrate id= "1" &gt;


&lt;lower&gt;0&lt;/lower&gt;


&lt;upper&gt;500&lt;/upper&gt;


&lt;rate&gt;5&lt;/rate&gt;


&lt;buckle&gt;0&lt;/buckle&gt;


&lt;/taxrate&gt;


&lt;taxrate id= "2" &gt;


&lt;lower&gt;500&lt;/lower&gt;


&lt;upper&gt;2000&lt;/upper&gt;


&lt;rate&gt;10&lt;/rate&gt;


&lt;buckle&gt;25&lt;/buckle&gt;


&lt;/taxrate&gt;


&lt;taxrate id= "3" &gt;


&lt;lower&gt;2000&lt;/lower&gt;


&lt;upper&gt;5000&lt;/upper&gt;


&lt;rate&gt;15&lt;/rate&gt;


&lt;buckle&gt;125&lt;/buckle&gt;


&lt;/taxrate&gt;


&lt;taxrate id= "4" &gt;


&lt;lower&gt;5000&lt;/lower&gt;


&lt;upper&gt;20000&lt;/upper&gt;


&lt;rate&gt;20&lt;/rate&gt;


&lt;buckle&gt;375&lt;/buckle&gt;


&lt;/taxrate&gt;


&lt;taxrate id= "5" &gt;


&lt;lower&gt;20000&lt;/lower&gt;


&lt;upper&gt;40000&lt;/upper&gt;


&lt;rate&gt;25&lt;/rate&gt;


&lt;buckle&gt;1375&lt;/buckle&gt;


&lt;/taxrate&gt;


&lt;taxrate id= "6" &gt;


&lt;lower&gt;40000&lt;/lower&gt;


&lt;upper&gt;60000&lt;/upper&gt;


&lt;rate&gt;30&lt;/rate&gt;


&lt;buckle&gt;3375&lt;/buckle&gt;


&lt;/taxrate&gt;


&lt;taxrate id= "7" &gt;


&lt;lower&gt;60000&lt;/lower&gt;


&lt;upper&gt;80000&lt;/upper&gt;


&lt;rate&gt;35&lt;/rate&gt;


&lt;buckle&gt;6375&lt;/buckle&gt;


&lt;/taxrate&gt;


&lt;taxrate id= "8" &gt;


&lt;lower&gt;80000&lt;/lower&gt;


&lt;upper&gt;100000&lt;/upper&gt;


&lt;rate&gt;40&lt;/rate&gt;


&lt;buckle&gt;10375&lt;/buckle&gt;


&lt;/taxrate&gt;


&lt;taxrate id= "9" &gt;


&lt;lower&gt;100000&lt;/lower&gt;


&lt;upper&gt;99999999&lt;/upper&gt;


&lt;rate&gt;45&lt;/rate&gt;


&lt;buckle&gt;15375&lt;/buckle&gt;


&lt;/taxrate&gt;


&lt;/taxrates&gt;

3. Read and display XML content

The code is as follows Copy Code

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>jquery Read and display XML content </title>
<script src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type= "Text/javascript" ></ Script>
<body>
<div id= "Error" ></div>
</body>

Css

<style>
#body {margin:0; padding:0; padding-top:10px;}
#book {width:98% height:auto; margin:0 auto; font-size:12px; text-indent:10px; color: #333; line-height:20px; Border-bottom:solid 1px #ccc; padding-bottom:10px; margin-bottom:10px; }
#book. Price {font-family: "Microsoft Ya hei"; font-size:14px; font-weight:900; color: #f00;}
#error {width:98%; height:36px; line-height:36px; margin:0 auto; margin-bottom:10px; color: #fff; font-size:12px; Text-align:left; text-indent:20px; Background: #f60; Display:none; }
#error a {text-decoration:none; color: #fff;}
#error a:hover {Text-decoration:underline}
</style>

Jquery

The code is as follows Copy Code

&lt;script&gt;


Load the following when page load completes


$ (window). Load (function () {


Loadxml ();


});


Var


Bid


Btitle,


Bname,


Bdescription,


Bprice,


Btime,


html


Load and display XML element content


function Loadxml () {


$ (' #error '). Text (' Loading book contents, please wait ... '). Show ();


$.ajax ({


URL: ' Book.xml ',


Cache:false,


DataType: "xml",


Type: ' Get ',


timeout:4000,


Error:function (data) {


$ (' #error '). HTML (' ERROR: Book list failed to load! &lt;a href= "javascript:;" onclick= "Loadxml ();" &gt; Click Reload &lt;/a&gt; ');


},


Success:function (data) {


$ (' Body #book '). Remove ();


$ (data). Find (' BS '). each (function (i) {


Bid = $ (this). attr (' id '); Using the attr method to get the BS element ID


Btitle = $ (this). Children (' title '). Text (); Gets the child element content of the current element by using the children method


Bname = $ (this). Children (' name '). text ();


Bdescription = $ (this). Children (' description '). Text ();


Bprice = $ (this). Children (' price '). Text ();


Btime = $ (this). Children (' time '). text ();


html = ' &lt;div id= ' book ' style= ' Display:none; &gt; '


+ ' &lt;div&gt; #: ' +bid+ ' &lt;/div&gt; '


+ ' &lt;div&gt; title: ' +btitle+ ' &lt;/div&gt; '


+ ' &lt;div&gt; Author: ' +bname+ ' &lt;/div&gt; '


+ ' &lt;div&gt; introduction: ' +bdescription+ ' &lt;/div&gt; '


+ ' &lt;div&gt; Price: ¥ ' +bprice+ ' &lt;/div&gt; '


+ ' &lt;div&gt; time: ' +btime+ ' &lt;/div&gt; '


+ ' &lt;/div&gt; ';


$ (' body '). Append (HTML); Append content to Body


$ (' Body #book '). Slidedown (' fast '); Slide down Pull effect display


});


$ (' #error '). html (' book List loaded successfully! A total of ' +$ (' body #book ') was loaded. Size () + ' This book &lt;a href= ' javascript:; "onclick=" Loadxml (); " &gt; Click Reload &lt;/a&gt; ');


}


});


}


&lt;/script&gt;

The returned XML document

The code is as follows Copy Code

&lt;?xml version= "1.0" encoding= "Utf-8"?&gt;


&lt;! DOCTYPE Note SYSTEM "NOTE.DTD" &gt;


&lt;b:book xmlns:b= "http://localhost" &gt;


&lt;bs id= "1" &gt;


&lt;title&gt; How steel is tempered &lt;/title&gt;


&lt;name&gt; Nicolas Ostrovsky &lt;/name&gt;


&lt;description&gt; How steel is tempered is a novel by former Soviet writer Nicolas Ostrovsky ... &lt;/description&gt;


&lt;price&gt;39.9&lt;/price&gt;


&lt;time&gt;2012-03-20&lt;/time&gt;


&lt;/bs&gt;


&lt;bs id= "2" &gt;


&LT;TITLE&GT;2015 New Postgraduate English reading comprehension 150 &lt;/title&gt;


&lt;name&gt; Mr. Zeng, Zhang Jian, Liu Xiaoying King Orchid School &lt;/name&gt;


&lt;description&gt; Zhang Jian, ph. D., graduated from the Department of British Literature at Glasgow University, professor of English College, Beijing Foreign Studies University, and associate dean of English school ...&lt;/description&gt;


&lt;price&gt;31.30&lt;/price&gt;


&lt;time&gt;2012-03-15&lt;/time&gt;


&lt;/bs&gt;


&lt;bs id= "3" &gt;


&lt;title&gt; Cultural Bitter Tour (New edition) (with Calligraphy edition) &lt;/title&gt;


&lt;name&gt; Yu Qiuyu &lt;/name&gt;


The Book of &lt;description&gt; Culture Bitter Brigade was first published in 1992, which was written by Yu Qiuyu in his lectures and expeditions at home and abroad in the 1980 ...&lt;/description&gt;


&lt;price&gt;27.60&lt;/price&gt;


&lt;time&gt;2012-03-17&lt;/time&gt;


&lt;/bs&gt;


&lt;bs id= "4" &gt;


&lt;title&gt; Lung response to Taiwan's family of Works series (a total of 3 albums) &lt;/title&gt;


&lt;name&gt; Dragon Counter, Andre &lt;/name&gt;


&lt;description&gt; 15 years ago, the Dragon's desk wrote "Baby You take your time" with a mother's personal experience ...&lt;/description&gt;


&lt;price&gt;63.50&lt;/price&gt;


&lt;time&gt;2012-03-16&lt;/time&gt;


&lt;/bs&gt;


&lt;bs id= "5" &gt;


&lt;title&gt; the great people who influence the world Big 32 open 20 volumes &lt;/title&gt;


&lt;name&gt; Translation Center Book franchise Store &lt;/name&gt;


&lt;description&gt; every great man with the world's influence has a deeply moving story. Their rich experience and life experience ...&lt;/description&gt;


&lt;price&gt;355.00&lt;/price&gt;


&lt;time&gt;2012-03-19&lt;/time&gt;


&lt;/bs&gt;


&lt;/b:book&gt;

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.