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>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Malo
  • 1,232
  • 2
  • 17
  • 28

3 Answers3

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);
    }
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 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 – Malo May 07 '15 at 12:17
  • 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. – Rory McCrossan May 07 '15 at 12:18
  • okay you are right i was wrong because i put ArrayOfMobileConfiguration instead of MobileConfiguration, Regards @Rory McCrossan – Malo May 07 '15 at 12:57
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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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);
}
});
Manu Benjamin
  • 987
  • 9
  • 24