PDA

View Full Version : Associative arrays parsing XML



msomers
August 8th, 2007, 06:43 AM
Hi guys

I need to parse an XML doc into an associative array (or maybe there are methods in the XML that can be used to locate position - other than child and parent)

I’m finding it difficult to output individual tag elements how can this be done (using items ???)

Any idea or code examples that are currently out there… current documents don’t detail with placing data in arrays or Im I wrong :shifty:

drop2
August 8th, 2007, 10:44 AM
I am using this way might be helpfull:




//************************************************** *********************
// XML TO ARRAY FUNCTION
//************************************************** *********************
xmlToArray = function (xmlStr) {
var resultArray = new Array();
var j = 0;
for (var i = 0; i<xmlStr.childNodes.length; i++) {
if (xmlStr.childNodes[i].nodeType == 1) {
resultArray[j] = xmlToArray(xmlStr.childNodes[i]);
resultArray[j]["name"] = xmlStr.childNodes[i].nodeName;
/// Check Attributes
resultArray[j]["attributes"] = new Array();
var z = 0;
for (attr in xmlStr.childNodes[i].attributes) {
resultArray[j]["attributes"][z] = new Array();
resultArray[j]["attributes"][z][attr] = xmlStr.childNodes[i].attributes[attr];
z++;
}
} else if (xmlStr.childNodes[i].nodeType == 3) {
resultArray["text"] = xmlStr.childNodes[0].nodeValue;
/// Check Attributes
resultArray[j]["attributes"] = new Array();
var z = 0;
for (attr in xmlStr.childNodes[i].attributes) {
resultArray[j]["attributes"][z] = new Array();
resultArray[j]["attributes"][z][attr] = xmlStr.childNodes[i].attributes[attr];
z++;
}
}
j++;
}
return resultArray;
};
//Example: array = xmlToArray(this);


Hi guys

I need to parse an XML doc into an associative array (or maybe there are methods in the XML that can be used to locate position - other than child and parent)

I’m finding it difficult to output individual tag elements how can this be done (using items ???)

Any idea or code examples that are currently out there… current documents don’t detail with placing data in arrays or Im I wrong :shifty:

senocular
August 8th, 2007, 11:04 AM
position is indicated by childIndex()