PDA

View Full Version : Code example: xml to xml code literal object


luxe
04-14-2007, 02:49 PM
ok. this AS script converts an XML object into an object array and then into a string literal version of the object array

so...

'<node id="012"><childNode id="789" label="someLabel"></childNode></node>'

... will give you ...

'{nodeName:"node", nodeValue:"null", attributes:{id:012}, childNodes:[{nodeName:"childNode", nodeValue:"null", attributes:{label:"someLabel",id:789}, childNodes:[]}]}' ...as a string!


So whats the point? the point is that you can then use the string literal code to be written in a JSFL file and then read the file again which JSFL will understand as an object array. Inside the JSFL file you can use AS syntax to move through the heirarchy. e.g. xmlObj.childNodes[0].childNodes[2].nodeName
or xmlObj.childNodes[0].childNodes[2].attributes.label.

So heres the code. The codes divided into two functions: one that converts an xml object into and object array. And the other that converts the object array into a code literal version of it. There is much room for improvement so feel free to do so and plz post it when you have. wicked!

----------------------------------------------------------------------

var xmlString = '<node id="012"><childNode id="789" label="someLabel"></childNode></node>';
var xmlData = new XML(xmlString);
var xmlObj = new Object();

/*
convert xml to object array.
*/
xml_to_Obj(xmlData);

// >> trace(xmlObj);

function xml_to_Obj(xml, obj):Void {

var i;
if (!obj) {
// create new root object
var xo = new Object();
xo.nodeName = xml.firstChild.nodeName;
xo.childNodes = new Array();
xo.attributes = new Object();

obj = xo;
}

if (xml.nodeType == 1) {

// recurse child nodes
for (i in xml.childNodes) {

var xo = new Object();
xo.nodeName = xml.childNodes[i].nodeName;
xo.nodeValue = xml.childNodes[i].nodeValue;
xo.childNodes = new Array();
xo.attributes = new Object();

obj.childNodes[i] = xo;

xml_to_Obj(xml.childNodes[i], obj.childNodes[i]);


}
} else if (xml.nodeType == 3) {

// get text node
obj.nodeValue = xml.nodeValue;
}

// get attributes
for (i in xml.attributes) {
obj.attributes[i] = xml.attributes[i];

}
xmlObj = obj;

}



/*
Convert from object array to JSFL / AS literal code
*/
var xml_literalString = '';
obj_to_literal(xmlObj);

// ok. for some reason it adds an additional ']}' at the end so for now im just trimming it off. times short so...
xml_literalString = xml_literalString.slice(0, xml_literalString.length - 2);

// >> trace(xml_literalString);

function obj_to_literal(xmlObj):Void {

var i = 0;

// recurse child nodes
while(i < xmlObj.childNodes.length) {

// add seperator
if(i!=0) {
xml_literalString += ',';
}

// get node attributes
var attStr = getAttributes(xmlObj.childNodes[i]);

// concact string. (hard coded. - this should be a loop)
xml_literalString += '{nodeName:"'+xmlObj.childNodes[i].nodeName+'", nodeValue:"'+xmlObj.childNodes[i].nodeValue+'", attributes:{'+attStr+'}, childNodes:[';

obj_to_literal(xmlObj.childNodes[i]);

i++;
}
xml_literalString += ']}';
}

// get node attributes and returns them as a string
function getAttributes(obj):String {

var attributeStr = '';
var i = 0;

for(var prop in obj.attributes) {

// add seperator
if(i > 0) {
attributeStr += ',';
}

// check if attribute is a string or number
var node_attribute = obj.attributes[prop];

if( isNaN(node_attribute) ) {
// is a string. add commas
attributeStr += prop+':"'+node_attribute+'"';
} else {
// is a number. dont add commas
attributeStr += prop+':'+node_attribute;
}

i++;
}

return attributeStr;
}

luxe
04-14-2007, 04:54 PM
OH...you can follow up here: http://www.mediasparkles.com/2004_02_01_archives.html

to see how to 'include' JSFL vars from an external file. Assuming that the JSFL Object Array is been saved to an external JSFL file.