利用jQuery中的filter来过滤重复结构的xml数据

 

 

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <bookstore>
 3     <book>
 4         <title>Rain</title>
 5         <year>2010</year>
 6         <author>Martin</author>
 7     </book>
 8     <book>
 9         <title>Cloud</title>
10         <year>2009</year>
11         <author>Robert</author>
12     </book>
13     <book>
14         <title>River</title>
15         <year>2007</year>
16         <author>Fred</author>
17     </book>
18 </bookstore>

引入jQuery如下:

<script type="text/javascript" src="Scripts/jquery-1.4.1-vsdoc.js"></script>
<script type="text/javascript" src="Scripts/test.js"></script>

 

test.js的代码如下:

$(document).ready(function () {
    $.get("xmlData/Categories.xml", function (xml) {
                   $(xml).find("book").filter(
                    function (index) {
                        return "Rain" == $(this).find("title").text();
                    }
                   ).each(
                    function (index) {
                        alert($(this).find("author").text());
                            });
    });
});

输出结果:Martin

 

posted @ 2012-08-10 22:52  风影极光  阅读(1189)  评论(0编辑  收藏  举报