Mustapha George
Mustapha George

Reputation: 2527

jquery extract values from XML

How can I show all price/qty combinations from this SQL string returned from a jQuery Ajax call?

    <transactions> 
      <transaction> 
          <price>999.99</price>
          <qty>999</qty>
       <transaction>
    </transactions>

JavaScript code:

$.ajax({
    url: 'myURL',
        dataType: 'xml',
         type:'POST',       
         data:  'data=' + someData, 
         success: function(xml){

    $(xml).find('transactions').each(function() {
         alert(something);
    }); 

Upvotes: 1

Views: 334

Answers (3)

Rafay
Rafay

Reputation: 31033

always parse the xml e.g. in jquery you can use parseXML using the DOM traversal methods is browser dependent

var xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$price = $xml.find("price").text();
console.log($price );

http://jsfiddle.net/3nigma/KhNCV/15/

Upvotes: 0

Niels
Niels

Reputation: 49919

Try this http://api.jquery.com/jQuery.parseXML/

var xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$price = $xml.find( "price" ),
$qty= $xml.find( "qty" );

Upvotes: 1

ShankarSangoli
ShankarSangoli

Reputation: 69905

Use jQuery parseXML before traversing it. Try this

$.parseXML(xml).find('transactions').each(function() {
     alert($(this).find('price'));//Alerts price
     alert($(this).find('qty'));//Alerts qty
}); 

Upvotes: 0

Related Questions