Mike Uistervet
Mike Uistervet

Reputation: 73

how to retrieve xml child nodes using jquery

I'm having a hard time retrieving the xml when its formatted the way it is below, I can retrieve InventoryResponse just fine but when trying to get the child a:Costit's not working. I tried to do research but haven't seen any examples that work in my situation. What is the simplest way to achieve getting the cost?

 $.ajax({
      type: 'POST',
      url: 'php/test.php',
      dataType:"xml",
      data: {
        search:'set',
      },

      success: function(data){

    var xml =  $(data).find('InventoryResponse').html() ;
    var cost=  $(data).find('a:Cost').html() ;

    $('.display').html(cost) ; 
      }
    })

test.php file returning xml

<InventoryResponse>
  <Inventories>
    <a:Inventory>
       <a:ID>
         12345
       </a:ID>
       <a:Cost>
         20.00
       </a:Cost>
     </a:Inventory>
   </Inventories>
</InventoryResponse>

Upvotes: 0

Views: 255

Answers (2)

Uttam Gupta
Uttam Gupta

Reputation: 418

Please try below code

var parser, xmlDoc;
    var text = "<InventoryResponse><Inventories><a:Inventory><a:ID>12345</a:ID><a:Cost>20.00</a:Cost></a:Inventory></Inventories></InventoryResponse>";

    parser = new DOMParser();
    xmlDoc = parser.parseFromString(text, "text/xml");


    alert(xmlDoc.getElementsByTagName("InventoryResponse")[0].childNodes[1].childNodes[0].childNodes[1].innerHTML);

Upvotes: 1

Jirka Jr
Jirka Jr

Reputation: 41

what about this?

var cost=  $(data).find('a\\:Cost').html() ;

Upvotes: 1

Related Questions