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.
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(
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!");
}
}
);
This worked in a fiddle:
$($xml).find('node').children().each(
function(){
alert( $(this).attr('name') );
}
);
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/
精彩评论