I want to extract all the data from the XML which has text nodes that is present in a variable and create an object array. Using Jquery for the same.
I have the below XML data.
var header = ['name', 'data1', 'data2'];
var data = '<parent1>' +
'<person>' +
'<name>Name1</name>' +
'<details>' +
'<data1>123</data1>' +
'<data2>34567</data2>' +
'</details>' + '</child>' + '<person>' +
'<name>Name1</name>' +
'<details>' +
'<data1>123</data1>' +
'<data2>34567</data2>' +
'</details>' + '<person>' + '</parent1>';
xmlDoc = $.parseXML( data ),
$xml = $( xmlDoc ),
var tabData = [];
var obj = {};
$xml.find('parent1').each(function(item, index){
header.forEach(function (item, index) {
$t = $xml.find(item).text();
obj[item] = $t;
});
tabData.push(obj);
obj = {};
The object should contain
{name : Name1, data1 :123, data2:34567}, {name : Name2, data1 :123, data2:34567}.
The loop is not having the access to the textnodes. $this does not help me get the search done for the individual child as well.
Requirement is the function should be dynamic and should work any type of the XML trees.
Could anyone please help.