2

I need to parse the XML response returned by a web service in ajax, this is my code, 'response' is the response returned by the web service, how do I create a XML object and parse it?

$.ajax({
    type: 'POST',
    url: 'web service link',
    dataType: 'xml:lang',
    success: function (response) {
        // how to parse the response here
    },
    error: function (error) {
        console.log(error);
    }
});

This is my XML code:

<ArrayOfMobileConfiguration xmlns:xsd="w3.org/2001/XMLSchema"; xmlns:xsi="w3.org/2001/XMLSchema-instance"; xmlns="tempuri.org/">; 
    <MobileConfiguration> 
        <Id>1</Id> 
        <Key>STMaxDownloadSize</Key> 
        <Value>132000</Value> 
    </MobileConfiguration> 
    <MobileConfiguration> 
        <Id>2</Id> 
        <Key>ZoomingThresholdValue</Key> 
        <Value>14</Value> 
    </MobileConfiguration>
</ArrayOfMobileConfiguration>
2

3 Answers 3

4

jQuery is able to retrieve values from an XML response in the same manner it would select standard HTML. Try this:

$.ajax({
    type: 'POST',
    url: 'web service link',
    dataType: 'xml',
    success: function (response) {
        $('MobileConfiguration', response).each(function() {
            var id = $(this).find('Id').text();
            var key = $(this).find('Key').text();
            var value = $(this).find('Value').text();

            console.log(id, key, value);
        });
    },
    error: function (error) {
        console.log(error);
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I tried to execute this code, and its working but if i make alert(Id) into the function it display : 12 and alert(key) display (STMaxDownloadSizeZoomingThresholdValue) it means that the values of Id and key are the value of xml tags concatenated each other, how can i loop for each one of xml tag and do something with the value ? thank u
This code is looping over those elements so should work fine. It would probably be best for you to start a new question including the code you're using.
okay you are right i was wrong because i put ArrayOfMobileConfiguration instead of MobileConfiguration, Regards @Rory McCrossan
0

This:

dataType: 'xml:lang',

should be just:

dataType: 'xml',

jQuery will then ignore the content-type of the response and parse it as XML before populating the variable you have named response in the success function with it.

Comments

0

Try the following code,

$.ajax({
type: 'POST',
url: 'web service link',
dataType: 'xml',
success: function (response) {
   $(response).find('MobileConfiguration').each(function(){
        var Id = $(this).find('Id').text();
        var Key = $(this).find('Key').text();
        var Value = $(this).find('Value').text();

        // Use Id, Key, Value according to your requirement.
   });
},
error: function (error) {
    console.log(error);
}
});

Comments

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.