0

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.

2
  • 1
    Nobody can help unless you tell how your xml looks like. Commented Jun 7, 2011 at 16:46
  • @TheSuperTramp am trying to fix it but editing on an iPhone is a PITA... Commented Jun 7, 2011 at 16:48

4 Answers 4

4

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(
Sign up to request clarification or add additional context in comments.

Comments

1

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!");
                    }
                }
            );

Comments

1

This worked in a fiddle:

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

                }
            );

Comments

1

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/

1 Comment

Thank you all very much for your insight. I will look into all your suggestions asap

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.