开发者

select xml child element with jQuery

Is it possible to find an xml element by searching with the value of a child element?

So for XML such as:

<route>
    <name>Lakeway</name>
    <abrv>LAKE</abrv>
    <eta>
        <time>00:30</time>
        <dest>MAIN</dest>
    </eta>
</route>

If I only have the value of the abrv, how would I parse the xml so as开发者_StackOverflow to get at the eta tags' child elements?

Is this doable with jQuery without looping through each element and comparing the values to the defined variable? Or is there a better way to do this?

EDIT:

I am trying to parse an XML feed from another domain. I'm not sure if I can do that with the xmlParse function, or if I'll need to use jQuery's Ajax functions?


You could use the contains filter to filter nodes based on this value.

Something like,

$(xml).find('abrv:contains(LAKE) + eta > *')

will return a jQuery object with two elements for the sample document.

<time>00:30</time>
<dest>MAIN</dest>

Here's an example.


Maybe something like this:

var xmlString = "<route><name>Lakeway</name><abrv>LAKE</abrv><eta><time>00:30</time><dest>MAIN</dest></eta></route>",
xmlDoc = $.parseXML( xmlString ),
$xml = $( xmlDoc ),
$abrv = $xml.find( "abrv" ).text() == "LAKE" ? $xml.find( "abrv" ) : false;

var time = $abrv.next().children("time").text();
var dest = $abrv.next().children("dest").text();

alert(time + " " + dest);

http://jsfiddle.net/jensbits/UH2m5/

Hopefully, that could get you started.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜