admin管理员组

文章数量:1415059

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/2001/XMLSchema"; xmlns:xsi="w3/2001/XMLSchema-instance"; xmlns="tempuri/">; 
    <MobileConfiguration> 
        <Id>1</Id> 
        <Key>STMaxDownloadSize</Key> 
        <Value>132000</Value> 
    </MobileConfiguration> 
    <MobileConfiguration> 
        <Id>2</Id> 
        <Key>ZoomingThresholdValue</Key> 
        <Value>14</Value> 
    </MobileConfiguration>
</ArrayOfMobileConfiguration>

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/2001/XMLSchema"; xmlns:xsi="w3/2001/XMLSchema-instance"; xmlns="tempuri/">; 
    <MobileConfiguration> 
        <Id>1</Id> 
        <Key>STMaxDownloadSize</Key> 
        <Value>132000</Value> 
    </MobileConfiguration> 
    <MobileConfiguration> 
        <Id>2</Id> 
        <Key>ZoomingThresholdValue</Key> 
        <Value>14</Value> 
    </MobileConfiguration>
</ArrayOfMobileConfiguration>
Share Improve this question edited May 7, 2015 at 10:55 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked May 7, 2015 at 10:50 MaloMalo 1,6164 gold badges22 silver badges36 bronze badges 2
  • Try DomParser. – Nilay Vishwakarma Commented May 7, 2015 at 10:55
  • stackoverflow./questions/17604071/parse-xml-using-javascript – Garry Commented May 7, 2015 at 11:00
Add a ment  | 

3 Answers 3

Reset to default 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);
    }
});

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.

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);
}
});

本文标签: jqueryparse xml response of web service using javascript and ajaxStack Overflow