0

I would like to use Ajax to check if an XML string ("data") contains a certain value under "voteBy", e.g. the name "John Doe". If yes, then I want to show an alert, if not then show another alert. The idea behind is that I want to do something here later with jQuery but only if the search term is NOT found in the XML string.

I tried the below which is the success function of the Ajax call to fetch this XML string. In this case it should alert "found" as the search term appears in the XML string but my approach doesn't return anything.

Can someone tell me what I am doing wrong here? Note: Data for the XML can vary so there may be more or less or no votes for a certain item.

My XML looks as follows (example data, shortened):

<ranks>
  <itemDetails>
    <itemID>1</itemID>
    <title>Test item</title>
    <details>&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit...&lt;/p&gt;</details>
    <lastUpdate>Added</lastUpdate>
    <modTime>Saturday, 23 August 2014</modTime>
    <modBy>Someone</modBy>
    <comments>
      <commentID>1</commentID>
      <comment>Some comment</comment>
    </comments>
    <comments>
      <commentID>2</commentID>
      <comment>Another comment</comment>
    </comments>
    <votes>
      <voteBy>John Doe</voteBy>
    </votes>
    <votes>
      <voteBy>Jane Doe</voteBy>
    </votes>
  </itemDetails>
</ranks>

My jQuery (success function only, shortened) - data here is my XML string:

$.ajax({
    // ...
    success:function(data) {    
        if(!$(data).find('voteBy').text() == 'John Doe') {
            alert('not found');
        } else {
            alert('found');
        }
    }
});

1 Answer 1

3

one way is using .each element for xml nodes :

var yourXML;     
function search_xml(string_for_search){
  $.ajax({
     type:"GET",
     url: "xml_file.xml",
     dataType: "xml",
     success: function(xml){

          $(xml).find('voteBy').each(function(){
          if($(this).text() == string_for_search) alert ('found :)');
          else alert ('not found ! :(');
          });

        }
  });
}

search_xml("John Does");

another way : using .filter and .find

var yourXML;     
function search_xml(string_for_search){
  $.ajax({
     type:"GET",
     url: "xml_file.xml",
     dataType: "xml",
     success: function(xml){
              // Filter 
            myXML = $(xml).find("itemDetails").filter(function() {
                return $(this).find('voteBy').text() == string_for_search;
            });

             // Store a string with your keywords 
            var output = myXML.children().map(function() {
                return this.tagName + '=' + $(this).text();
            }).get().join(' ');

            alert(output);
        }
  });
}

search_xml("John Doe");
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot - this looks good ! Instead of storing the results is there a way to just alert something depending on whether the search term is found or not ?
@user2571510 accept my answer if your problem solved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.