Jquery筛选具有相同名称

时间:2017-06-26 14:03:31

标签: jquery xml filter

我试图从具有相同名称的不同节点的XML文件中提取信息。程序员最初输入的方式,它来自一个易于推断的单个节点。我之前修改过代码以将单个方面分离为cookie,但现在我需要进一步研究。我需要使用复杂的过滤器来识别最多5个不同节点的值,并将它们推送到不同的变量。本质上,我需要它从名为daypart的节点中选择值,其中daypart name = something(我是一个SQL人员,而不是Web管理员)。

我不太了解jquery,但我知道有一种方法可以使用过滤器。我已经包含了xml文件的片段,因此您可以看到。我需要在当前日期名称=(在这种情况下)早餐的地方拉出carserved和nummetgoal。任何帮助将不胜感激。

    <LaneMetrics>
<DriveThruLane1ServiceGoal>
  <AllDayParts>
    <DayPart>
      <AverageServiceTime>03:41</AverageServiceTime>
      <RecentAverageServiceTime>03:15</RecentAverageServiceTime>
      <TargetServiceTime>03:00</TargetServiceTime>
      <AverageServiceTimeSeconds>222</AverageServiceTimeSeconds>
      <RecentAverageServiceTimeSeconds>195</RecentAverageServiceTimeSeconds>
      <TargetServiceTimeSeconds>180</TargetServiceTimeSeconds>
      <PercentOnTarget>44%</PercentOnTarget>
      <CarsServed>81</CarsServed>
      <NumMetGoal>36</NumMetGoal>
      <ReportingName>DriveThruLane 1</ReportingName>
      <CurrentDaypartName>Breakfast</CurrentDaypartName>
      <CurrentDaypartCategory>ServicePeriod</CurrentDaypartCategory>
    </DayPart>
    <DayPart>
      <AverageServiceTime>03:58</AverageServiceTime>
      <RecentAverageServiceTime>02:43</RecentAverageServiceTime>
      <TargetServiceTime>04:00</TargetServiceTime>
      <AverageServiceTimeSeconds>238</AverageServiceTimeSeconds>
      <RecentAverageServiceTimeSeconds>163</RecentAverageServiceTimeSeconds>
      <TargetServiceTimeSeconds>240</TargetServiceTimeSeconds>
      <PercentOnTarget>57%</PercentOnTarget>
      <CarsServed>14</CarsServed>
      <NumMetGoal>8</NumMetGoal>
      <ReportingName>DriveThruLane 1</ReportingName>
      <CurrentDaypartName>Lunch</CurrentDaypartName>
      <CurrentDaypartCategory>ServicePeriod</CurrentDaypartCategory>
    </DayPart>

1 个答案:

答案 0 :(得分:0)

<强> JQuery的: 尝试 .each 循环遍历XML并找到匹配的值。

您可以使用以下循环:

$(xml).find('DayPart').each(function(index){
    if($(this).find('CurrentDaypartName').text() == "Breakfast"){
        alert("carsserved :" + $(this).find('CarsServed').text() + " and NumMetGoal : " + $(this).find('NumMetGoal').text())
    }  
 });

<强> JavaScript的:

xml = xml.getElementsByTagName('DayPart');

for(var i=0; i < xml.length;i++){    
    if(xml[i].getElementsByTagName('CurrentDaypartName')[0].innerHTML == "Breakfast"){
        alert("carsserved :" + xml[i].getElementsByTagName('CarsServed')[0].innerHTML + " and NumMetGoal : " + xml[i].getElementsByTagName('NumMetGoal')[0].innerHTML)
    }   
}