After fetching data from an XML document and storing them in three variables - the root element name, an array containing all the names of the root's children, and a second array with the length of those children sub-nodes, I am trying to convert these variables into a JSON object in the following format:
{ "root_name": {
"childName[0]": "lengthSubNodes[0]",
"childName[1]": "lengthSubNodes[1]",
"childName[2]": "lengthSubNodes[2]"
}
using this function:
function XMLtoJSON(rootName, childNames, childNumbers){
var xmlObject = {};
xmlObject[rootName] = {};
for(var i = 0; i < childNames.length; i++ ){
xmlObject[rootName][childNames[i]] = childNumbers[i];
}
}
While everything works correctly, I encounter an issue when dealing with an XML document that has multiple children with the same name and length. They appear only once like this:
{ "catalog": {
"book": 6
}
instead of how it should look like:
{ "catalog": {
"book": 6,
"book": 6,
"book": 6
}
Is there a way to address this problem?