[SWF6] Prototype - Convert XML to Array Object Tree Converts XML data for easy referencing.
-------------------------------------------------------------------------------------
EDIT: 12/19/06
I recently updated this code. Took it from swf6 to swf8. I added an aot2xml function as well as some xml and aot display functions, aotToDisplayString and xmlToDisplayString, to better format the data.
An example is attached to post #8: http://www.swish-db.com/forum/index.php?s=...st&p=173599
-------------------------------------------------------------------------------------
DESCRIPTION: This is a prototype to convert a valid XML string into an array object tree similar to the SWiSHmax movie's directory tree. Node attributes are placed in an 'attributes' object and direct references are made in the parent node for children with id attributes set. Case sensitivity is strict.
This method sets up a new property on the XML object, an aot (array object tree) which is literally an object model of the XML data loaded. It gives this object the nodeName of the XML's first child.
This work was originally started in Flash by J.Milkins and M. Ziebell but was considerably modified here for use in SWiSHmax.
CODE:
CODE XMLNode.prototype.xml2aot = function(xo) { // make array object tree // based on work by J.Milkins and M.Ziebell if(xo==undefined)xo=this; var c,cNode,nName,nType,cId,o=new Object(); o.attributes=new Object(); var oa=o.attributes; var xa=xo.attributes; for(c in xa)oa[c]=xa[c]; for(c in xo.childNodes){ cNode=xo.childNodes[c]; nName=cNode.nodeName; nType=cNode.nodeType; if(nType==3)o._value=cNode.nodeValue; else if(nType==1 && nName!=null){ if(!(o[nName] instanceof Array)){ o[nName]=new Array(); o[nName].__resolve=function(f){return this[0][f];}; ASSetPropFlags(o[nName],null,1,1); } this.xml2aot(cNode); var so=cNode.aot; o[nName].unshift(so); cId=cNode.attributes.id; if(cId!="undefined"&&o[cId]!="")o[cId]=so; } } xo.aot=o; if(xo==this)xo[this.firstChild.nodeName]=this.firstChild.aot; }; ASSetPropFlags(XMLNode.prototype,['xml2aot'],1,1);
USAGE: var example1 = new XML(xmlString); // load XML example data example1.xml2aot(); // convert XML to Array Object Tree (AOT)
EXAMPLE: var xmlString = '<?xml version="1.0" encod ...(too big, see below)... ks></gsbSite>'; var example1 = new XML(xmlString); // load XML example data example1.xml2aot(); // convert XML to Array Object Tree (AOT)
gsbTrace("XML: "+example1); // Parsed XMl data string echo.
// AOT direct access gsbTrace("0 "+example1.gsbSite.home.content._value); gsbTrace("1 "+example1.gsbSite.home.content.attributes.type); gsbTrace("2 "+example1.gsbSite.home.content.attributes['type']);
gsbTrace("3 "+example1.gsbSite.links.others.GypsyTrader.href._value); gsbTrace("4 "+example1.gsbSite.links.others.link[0].href._value); gsbTrace("5 "+example1.gsbSite.links.others['GypsyTrader'].href._value);
gsbTrace("\nexample1.gsbSite.links.favorites.attributes:"); for(var attr in example1.gsbSite.links.favorites.attributes) { gsbTrace(" "+attr+" = "+example1.gsbSite.links.favorites.attributes[attr]); }
RESULTS: gsbTrace output from example: From: localhost movie started @ 41 XML: <?xml version="1.0" encod ...(too big, see below)... ks></gsbSite> 0 <p>Some html text can go here.</p> 1 HTML 2 HTML 3 http ://www.GypsyTrader.com 4 http ://www.GypsyTrader.com 5 http ://www.GypsyTrader.com
example1.gsbSite.links.favorites.attributes: a3 = 03 a2 = 02 a1 = 01
XML DATA: This is the full data string as set in the variable xmlString above:
CODE <?xml version="1.0" encoding="UTF-8"?> <!-- Simple organization for testing, file 1 --> <gsbSite> <home> <content type="HTML"> <![CDATA[<p>Some html text can go here.</p>]]> </content> </home> <about> <content type="text"> ...or regular text, whatever. </content> </about> <links> <favorites a1="01" a2="02" a3="03"> <link id="google"> <href>http://www.google.com</href> <desc>My favorite search engine.</desc> </link> <link id="yahoo" type="search"> <href>http://www.yahoo.com</href> <desc>My not so favorite search engine.</desc> </link> </favorites> <others> <link id="GypsyTrader"> <href>http://www.GypsyTrader.com</href> <desc>My favorite web site.</desc> </link> <link id="SWiSH-DB"> <href>http://www.swish-db.com</href> <desc>My favorite SWiSH community.</desc> </link> </others> </links> </gsbSite> */
gsb
|