使用jQuery解析XML文件的一部分

时间:2013-02-14 16:50:44

标签: javascript jquery xml xml-parsing

从文本输入收到“day”后,如何使用jQuery从同一天开始显示<product>的{​​{1}}?也许使用<availabilities>

.siblings()

这是我的成功:功能:

<?xml version='1.0' encoding='iso-8859-15'?>
<timetable>
  <date>
    <day>22-01-2013</day>
    <availabilities>
      <availability>
        <starttime>10:00</starttime>
        <endtime>13:00</endtime>
        <startplace>Funchal</startplace>
        <endplace>Funchal</endplace>
        <resource>Excursões na Madeira</resource>
        <idstartplace>18</idstartplace>
        <idendplace>18</idendplace>
        <idperiodoconsumo>13289</idperiodoconsumo>
        <idproduct>23</idproduct>
        <product>EXC_SANT</product>
        <idtimeperiod>11523</idtimeperiod>
        <idavailability>3561</idavailability>
      </availability>
      <availability>
        <starttime>10:00</starttime>
        <endtime>13:00</endtime>
        <startplace>Funchal</startplace>
        <endplace>Funchal</endplace>
        <resource>Excursões na Madeira</resource>
        <idstartplace>18</idstartplace>
        <idendplace>18</idendplace>
        <idperiodoconsumo>13290</idperiodoconsumo>
        <idproduct>33</idproduct>
        <product>foot</product>
        <idtimeperiod>11524</idtimeperiod>
        <idavailability>3593</idavailability>
      </availability>
      <availability>
        <starttime>10:00</starttime>
        <endtime>13:00</endtime>
        <startplace>Funchal</startplace>
        <endplace>Funchal</endplace>
        <resource>Excursões na Madeira</resource>
        <idstartplace>18</idstartplace>
        <idendplace>18</idendplace>
        <idperiodoconsumo>13289</idperiodoconsumo>
        <idproduct>22</idproduct>
        <product>VLT_ILHA</product>
        <idtimeperiod>11523</idtimeperiod>
        <idavailability>3561</idavailability>
      </availability>
    </availabilities>
  </date>
  <date>
      ...
      </date> 
...

我很感激一些帮助,我不知道该怎么做..:|

2 个答案:

答案 0 :(得分:1)

试试这个:

var product = $(this).find('product').text();

我想你必须把它放在这里:

if (day==decodeURI(iHash[3])){
  var product = $(this).find('product').text();
  alert('Date matches!');
}

答案 1 :(得分:0)

这应该适合你:

function parseStuff(data,toBeMatchedDate){

    productList=array();

    $(data).find('date').each(function(){


        var day = $(this).find('day').text();

        if (day==decodeURI(iHash[3]) && day==toBeMatchedDate)   //if date is matched
        {

             xmlProdcutList=$(this).find('product'); //all the products associated with this date


             $.each(function(i,thisProduct){
                         productList.push(thisProduct.text()); //store in the main array
               });

return productList;


        }
    });
}