简体   繁体   中英

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:Cost it'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>

那这个呢?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM