使用jquery解析XML的方法_javascript技巧

來源:互聯網
上載者:User

本文執行個體講述了使用jquery解析XML的方法,分享給大家供大家參考之用。具體方法如下:

一、xml檔案結構:books.xml

<?xml version="1.0" encoding="UTF-8"?><root> <book id="1">  <name>深入淺出extjs</name>  <author>張三</author>  <price>88</price> </book> <book id="2">  <name>鋒利的jQuery</name>  <author>李四</author>  <price>99</price> </book> <book id="3">  <name>深入淺出flex</name>  <author>王五</author>  <price>108</price> </book> <book id="4">  <name>java編程思想</name>  <author>錢七</author>  <price>128</price> </book></root>

二、頁面代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>jquery解析xml</title><script type="text/javascript" src="js/jquery-1.4.2.min.js"></script><script type="text/javascript"> $(function(){  $.post('books.xml',function(data){   //尋找所有的book節點   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+"<br>";   });   $('#mydiv').html(s);  }); });</script></head><body> <div id='mydiv'></div></body></html>

運行效果圖如下:

感興趣的讀者可以點此本站下載完整代碼。

更多關於jquery xml操作相關內容感興趣的讀者可查看本站專題:《jQuery操作xml技巧總結》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.