0

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?

asprin
  • 9,579
  • 12
  • 66
  • 119
  • can you share the xml sample – Arun P Johny Aug 26 '14 at 09:11
  • @ArunPJohny Sure, added the xml code (though truncated) – asprin Aug 26 '14 at 09:19
  • http://jsfiddle.net/arunpjohny/mkprdqt8/3/ – Arun P Johny Aug 26 '14 at 09:25
  • Thanks. If you put that as an answer, I'd be happy to accept it. Also, do you happen to know of any documentation on `parseXML()` so that I can go through it? – asprin Aug 26 '14 at 09:28
  • I just used to convert the xml string to a document object... if you use a ajax request with `dataType: 'xml'` jQuery will do it for you - http://api.jquery.com/jquery.parsexml/ – Arun P Johny Aug 26 '14 at 09:31
  • The link you gave doesn't really give much information. I was looking for something which would give more information about which methods that can be used with the xml object as well as the syntax to be used for those methods. – asprin Aug 26 '14 at 09:34
  • there was another issue with the filter fixed it in http://jsfiddle.net/arunpjohny/mkprdqt8/6/ - also answer is updated – Arun P Johny Aug 26 '14 at 09:39

1 Answers1

1

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

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531