开发者

jQuery not parsing nested XML correctly

I'm parsing data from my xml which looks like this:

<departuresData> 
<station> 
  <id>29411</id> 
  <name>Kúpalisko Matador</name> 
  <distance>182</distance> 
  <vehicle> 
    <number>180</number> 
    <time>10:26</time> 
    <time>10:56</time> 
    <target>Holíčska</target> 
    <lineid>289</lineid> 
    <type>BUS</type> 
  </vehicle> 
  <vehicle> 
    <number>180</number> 
    <time>10:17</time> 
    <time>10:47</time> 
    <target>Cintorín Petržalka</target> 
    <lineid>288</lineid> 
    <type>BUS</type> 
  </vehicle> 
</station> 
<station> 
  <id>30122</id> 
  <name>Kúpalisko Matador</name> 
  <distance>201</distance> 
  <vehicle> 
    <number>80</number> 
    <time>10:09</time> 
    <time>10:33</time> 
    <target>Kollárovo nám.</target> 
    <lineid>220</lineid> 
    <type>BUS</type> 
  </vehicle> 
  <vehicle> 
    <number>80</number> 
    <time>10:02</time> 
    <time>10:26</time> 
    <target>Kúpalisko Matador</target> 
    <lineid>222</lineid> 
    <type>BUS</type> 
  </vehicle> 
</station> 
<departuresData>

and my parsing jquery looks like:

success: function(xml) {
 $(xml).find("station").eac开发者_开发技巧h(function()
  {

    //vydolovanie hodnot
    var name = $(this).find("name").text();
    var distance = $(this).find("distance").text();
    $("#response").append("Zastavka:" + name + "<br />Vzdialenost:" + distance + "<br />");  //vypis

    $(this).find("vehicle").each(function(){

        var number = $(this).find("number").text();
        var target = $(this).find("target").text();
        var type = $(this).find("type").text();

            $("#response").append("Linka cislo: " + number + "<br />Smer: " + target + "<br />Druh: " + type + "<br />Časy:<br />");
        $(this).find("time").each(function()
        {
            $("#response").append($(this).text() + "<br />"); //vypis
        });

        $("#response").append("1<br /><br />"); //vypis

    });

    $("#response").append("<br />");
  });

}

but it only comes to distance, and when it has to look into each vehicle part for some reason it can't get info which is in number, time and so on. And also it not loops through all vehicle tags in one station tag.

Output looks like:

Zastavka:Kúpalisko Matador
Vzdialenost:198
Linka cislo: 
Smer: 
Druh: 
Časy:

1


Zastavka:Kúpalisko Matador
Vzdialenost:201
Linka cislo: 
Smer: 
Druh: 
Časy:

1


Actually, on looking closer, your first output:

Vzdialenost:198 

states that the distance is 198. However, your xml does not have a distance of 198. There's

<distance>182</distance> 

and

<distance>201</distance> 

so either you've pasted the wrong question or you're not actually reading from the right xml.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜