Reputation: 73
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>
Upvotes: 0
Views: 255
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