all groups > flash actionscript > march 2005 >
You're in the

flash actionscript

group:

Parsing XML into heirarchical Objects?


Parsing XML into heirarchical Objects? bgoulette612
3/25/2005 10:48:49 PM
flash actionscript:
Hello, all! I'm trying to parse an XML file into a collection of generic
Objects that maintain the same heirarchy that exists in the original XML. And I
can't. :-(

I can create Objects based on the desired nodes, but I can't seem to create an
Object as a property of a previously-created Object; hence, I lose the
heirarchy.

Here's my XML: (Code at the bottom)

And here's what little bit of script I have left: (Frustration! kills
Actionscript dead!) (Code at the bottom, too!)

Obviously, the generateObjects() function just creates a bunch of unrelated
Objects. What I'd like to get is something like this:

navtexts = {};
navtexts.course_map = "Course Map";
navtexts.help = "Help";
.
.
.
parts = {};
parts.section_1 = {};
parts.section_1.title = "Section 1";
parts.section_1.topic_1 = {};
parts.section_1.topic_1.title = "S1 Topic 1";
parts.section_1.topic_1.text_1 = "Text one goes here.";

And so on and so forth...I would greatly appreciate any suggestions!!! Thanks
in advance!



[XML --]

<bizmod>
<navtexts>
<course_map title="Course Map" />
<help title="Help" />
<captions_off title="Turn captions on" />
<captions_on title="Turn captions off" />
<topic title="Topic" />
<slide_of_1 title="Slide" />
<slide_of_2 title="of" />
<previous title="Previous" />
<next title="Next" />
<exit title="Exit" />
</navtexts>

<parts>
<section_1 title="Section 1">
<topic_1 title="S1 Topic 1">
<slide_1 title="S1 T1 Slide 1">
<text_1>
Text one goes here.
</text_1>
<bullets_1>
<bullet_1 text="Bullet 1 text" />
<bullet_2 text="Bullet 2 text" />
<bullet_3 text="Bullet 3 text" />
</bullets_1>
<text_2>
Text two goes here.
</text_2>
</slide_1>
</topic_1>
</section_1>
</parts>
</bizmod>

[AS --]

bizmodsXML = new XML();
bizmodsXML.ignoreWhite = true;

bizmodsXML.onLoad = function( success ) {
if( success ) {
// Since I don't actually want to make an object out of the root,
// I just pass its firstChild and go from there.
generateObjects( this.firstChild );
} else {
trace('Something has gone horribly wrong.');
}
}

bizmodsXML.load('bizmods_texts.xml');

_global.generateObjects = function( ob ) {
if( !ob ) ob = bizmodsXML;

for( var i = 0; i < ob.childNodes.length; i++ ) {
var tempNode = ob.childNodes[i];

if( tempNode.hasChildNodes ) {
generateObjects( tempNode );
}
}
}
Re: Parsing XML into heirarchical Objects? bgoulette612
3/26/2005 9:43:35 PM
I figured it out. Here's the function I came up with, just in case anyone else
is interested:



_global.generateObjects = function( ob, parent ) {
var theNode = ob;
var theParent = parent ? parent : _global;

for( var i = 0; i < theNode.childNodes.length; i++ ) {
var theChild = theNode.childNodes[i];

if( theChild.hasChildNodes ) {
// Create a new object as a property of theParent.
theParent[theChild.nodeName] = new Object();
if( theChild.firstChild.nodeType != 3 ) {
for( var a = 0; a < theAttributesList.length; a++ ) {
if( theChild.attributes[theAttributesList[a]] )
theParent[theChild.nodeName][theAttributesList[a]] =
theChild.attributes[theAttributesList[a]];
}
/*
Here's the recursion: pass theChild and its parent object back
to the function and drill down another level -- but only if
we're not dealing with a text node. Lather, rinse, repeat.
*/
generateObjects( theChild, theParent[theChild.nodeName] );
} else {
/*
Presumably, the primary text for each slide will be
substantive enough to make using an attribute unwieldly at
best: so instead, when we hit a text node, append its value as
a new property called "text".
*/
theParent[theChild.nodeName].text =
stripCharacters(theChild.firstChild.nodeValue);
}
}
}
}
AddThis Social Bookmark Button