jqueryxmlparsexml

$.parseXML()'s find() method - Using it multiple times


I'm parsing an XML string using $.parseXML() API. This the following code that I'm using:

success:function(data)
{
    $(data).find('TabName:contains("Current Year Forms")').each(function(){
             $(this).parent().find('IndexEntry:contains("A")').each(function(){
                 console.log($(this).text());
             });
    });
}

This is working fine, but I was wondering if I can somehow reduce the lines of code. I'm trying to put a condition TabName="Current Year" AND IndexEntry="A". So is there something like:

$(data).find('TabName:contains("Current Year Forms")').find('IndexEntry:contains("A")')

that would put the conditions in a single statement?

UPDATE

I did get to a point where I was able to use

$(data).find('TabName:contains("Current Year Forms")', IndexEntry:contains("A")')

but that just used the OR condition instead of AND. This makes me assume that there must be another syntax for AND condition.

XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <record>
        <Account>
            <NOR>34</NOR>               
            <TabName> Current Year Forms</TabName>              
            <IndexEntry>A</IndexEntry>
        </Account>
        <Account>
            <NOR>22</NOR>
            <TabName> Current Year Forms</TabName>
            <IndexEntry>C</IndexEntry>
        </Account>
        <Account>
            <NOR>40</NOR>               
            <TabName>Current Year Forms</TabName>
            <IndexEntry>C</IndexEntry>
        </Account>
     </record>
</dataset>

PS: On a sidenote, I can't seem to find detailed documentation regarding the parseXML(), like what other methods I can use with with its object, properties etc. Do we have anything like that listed somewhere?


Solution

  • I think you can use a mix of .has() & .find()

    $(data).find('Account').has('TabName:contains("Current Year Forms")').find('IndexEntry:contains("A")').each(function () {
        console.log($(this).text());
    });
    

    Demo: Fiddle