sisko
sisko

Reputation: 9900

jquery ajax issue

this is likely to endup being an easy fix so I'll apologize in advance for wasting your time. I have the following code:

$.ajax({
            url: "/room/" + $nodeid + "/rss.xml",
            dataType: "xml",
            success: function($xml){
                $($xml).find('node').each(
                    function(){
                        alert( $(this).attr('name') );

                    }
                );
            },
            failure: function(){
                alert('Ajax not responding!');
            }
        });

And I am trying to read the following xml:

<?xml version="1.0" encoding="UTF-8" ?><xml>
  <node>
    <Title name = 'Title'>Committtee Room 1</Title>
    <Theatre name = 'Theatre'>40</Theatre>
    <Classroom name = 'Classroom'>24</Classroom>
    <Boardroom name = 'Boardroom'>22</Boardroom>
    <Cabaret name = 'Cabaret'>24</Cabaret>

  </node>
</xml>

Can someone please enlighten me and tell me why my alert( $(this).attr('name') ); is not returning "Title", "Theatre" etc

Thanks.

Upvotes: 0

Views: 80

Answers (4)

msmafra
msmafra

Reputation: 1714

Your XML should be like this:

<?xml version="1.0" encoding="UTF-8" ?>
<list>
      <node>
        <Title name='Title'>Committtee Room 1</Title>
        <Theatre name='Theatre'>40</Theatre>
        <Classroom name='Classroom'>24</Classroom>
        <Boardroom name='Boardroom'>22</Boardroom>
        <Cabaret name='Cabaret'>24</Cabaret>
      </node>
</list>

Sorry I misread your code, this tutorial could help: http://think2loud.com/reading-xml-with-jquery/

Upvotes: 1

jk.
jk.

Reputation: 14435

This worked in a fiddle:

$($xml).find('node').children().each(
                function(){
                    alert( $(this).attr('name') );

                }
            );

Upvotes: 1

NateDSaint
NateDSaint

Reputation: 1524

I agree with lonesomeday, but you might also try looking for the entities inside of your existing node each, so that you still maintain the loop structure per node tag.

$($xml).find('node').each(
                function(){
                    alert($(this).children("Title").attr("name"));
                    // or put some conditional logic for the node like so
                    if ($(this).attr("awesome") == true) {
                      alert($(this).children("Title").attr("name")+" is awesome!");
                    }
                }
            );

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237827

You've found the node element. This doesn't a name attribute.

You need to find the child elements of node instead:

$($xml).find('node > *').each(

Upvotes: 4

Related Questions