仅显示某些类别的XML数据

时间:2014-02-16 01:48:16

标签: javascript jquery xml cordova

我希望我的Phonegap iPhone应用程序读取我在服务器上的XML文档,该文档与产品列表联系,并显示它们,但仅限于产品属于特定类别时。

目前这完全有效但显示所有产品:

XML示例:

    <item>
    <id>25579</id>
    <image><![CDATA[http://urlexample.com.au/wp-content/uploads/catablog/thumbnails/Speaker.jpg]]></image>
    <subimages>
    </subimages>
    <title><![CDATA[Product Name]]></title>
    <description><![CDATA[]]></description>
    <date>2014-02-04</date>
    <order>5</order>
    <link><![CDATA[http://www.productlink.com.au]]></link>
    <price><![CDATA[0]]></price>
    <product_code><![CDATA[]]></product_code>
    <categories>
        <category><![CDATA[Speaker]]></category>
        <category><![CDATA[Wireless]]></category>
    </categories>
</item>

这是我的代码:

     $.ajax({
           type: "GET",
           url: "http://www.test.com.au/accessories.xml",
           dataType: "xml",
            beforeSend : function() {$.mobile.loading('show')},
            complete    : function() {$.mobile.loading('hide')},
           success: parseXml
           });
           function parseXml(xml) {


                // alert(xml);
                $(xml).find('item').each(function(){

                    $("#datadock").append('<img src="' + $(this).find("image").text() + '" style="-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;float:left;margin:1px;height:auto" width="49%">');

                });

有些产品有不同的类别。

我怎样才能让它显示“Category =”Speaker“的产品。例如,”类别“下的某些项目没有多个类别。

1 个答案:

答案 0 :(得分:0)

category = 'Speaker';
$.ajax({
    type: "GET",
    url: "test.xml",
    dataType: "xml",
    beforeSend: function () {},
    complete: function () {},
    success: parseXml
});

function parseXml(xml) {



    $(xml).find("item").each(function () {

        if ($(this).find('category').text().indexOf(category) != -1) {

            $("#datadock").append('<img src="' + $(this).find("image").text() + '" style="-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;float:left;margin:1px;height:auto" width="49%">');

        }


    });
}