0

I have an xml file and I want to search some values from that file using jquery but the value that i get inside the function and outside the function are different please point me in the right direction.this is the xml file

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
  <dict>
    <key>ID</key>
    <string>B0A6EF3C-221F-4918-89C2-340B05F6A7AD</string>
    <key>Name</key>
    <string>name</string>
    <key>Items</key>
    <array>
      <dict>
        <key>Mode</key>
        <integer>1000</integer>
        <key>background</key>
        <string>RGBA:0.000000,1.000000,1.000000,1.000000</string>
        <key>Enabled</key>
        <true/>
      </dict>
      <dict>
        <key>Mode</key>
        <integer>1000</integer>
        <key>background</key>
        <string>RGBA:0.000000,1.000000,1.000000,1.000000</string>
        <key>Enabled</key>
        <true/>
      </dict>
    </array>
  </dict>
</plist>

the code that i used is

$.post("demo.xml",{},function(xml){

    $('array',xml).each(function(i) {
        $(this).find('dict').each(function(){
              var valueError = findvalue($(this),'Mode');
              alert(valueError);

            });
        });
});

function findvalue(tag,searchkey)
{
    $(tag).find('key').each(function(){

            key = $(this).text();
            value = $(this).next().text();
            //alert("inside = "+ value)
             if(key == searchkey)
               {
                alert("key = "+key + "  searchkey =  " + searchkey +" value =  " +value)
                    return value;
               }
               else
               {
                return "No Match";
               }
    }); 

}

when control inside the findvalue function it print correct value but when it going to Calling function and print the return value that is in this case is valueError it print undefined

1 Answer 1

1

It's returning undefined because you are returning the value from inside a callback function, so the main function itself is not returning a value.

You'll need to set the return value to a variable in the function and return that from the main function:

function findvalue(tag, searchkey) {
    var returnValue = false;
    $(tag).find('key').each(function(){
        key = $(this).text();
        value = $(this).next().text();
        if(key == searchkey) {
            alert("key = " + key + "  searchkey =  " + searchkey + " value =  " + value);
            returnValue = value; // assign to the parent functions variable
            return value; // only returns to the callback function
        } else {
            returnValue = "No Match"; // same as above
            return "No Match";
        }
    }); 

    return returnValue; // this will allow you to access that value now
}

It seems strange to me to return 'No Match' during a loop, perhaps you should look at simply removing that else statement and letting the parent function simply return false (default as defined at the top) when no match is found, and output "No Match" when you display the results (presumably where you are outputting the results of findvalue()).

Sign up to request clarification or add additional context in comments.

1 Comment

thanks to point me in right direction .yes you are right else is not necessary .now i remove the else part and the code work fine.thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.