Solved

Recursively convert XML into multidimensional array

Posted on 2010-09-17
6
596 Views
Last Modified: 2012-05-10
Hello all,

I have figured out how to run through an xml file with nested nodes recursively (please see code below). My challenge now is how do i convert it into a multidimensional array? I would like the array to be the data source for a Flex tree control. Please help!

Thanks,
tone

var xml:XML =     
	<course>         
		<section>             
			<title>Introduction to Actionscript</title>             
			<section>             
				<title>Lesson 1: Variables</title>             
				<section>                 
					<title>Topic 1: Data types</title>             
				</section>             
			</section>         
		</section>     
	</course>;             


var xmlList:XMLList = xml.children();                 


parseStructure(xmlList);     


private function parseStructure(xml:XMLList):void {    
	for each (var child:XML in xml) {         
		trace(child.title);         
		if(child.section != null){                     
			parseStructure(child.section);         
		}           
	} 
}

Open in new window

0
Comment
Question by:toneDigital
  • 4
  • 2
6 Comments
 
LVL 20

Expert Comment

by:ChristoferDutz
Comment Utility
Ok the code below is somewhat pseudocode as it doesn't handle that the first level has no "title" child. But I think it gets you the idea. The trick is to have the parseStructure return an Object for each "section" Xml tag. Each Object has a name and a children property (I think this was the default the Tree uses). The children property is initialized with all the child objects for that section.

Hope it helps.
var treeRoot:Object = parseStructure(xml);     



private function parseStructure(xml:XML):Object{    

    var obj:Object = new Object();

    obj.name = xml.title;

    if(child.section != null) {

        obj.children = new ArrayCollection();

        for each (var child:XML in xml) {

            obj.children.addItem(parseStructure(child));         

        }

    }

}

Open in new window

0
 

Author Comment

by:toneDigital
Comment Utility
Hi Christofer, thanks for your quick reply.

I am still a bit confused, although I know what you're saying is correct. I'm kind of new to the tree control idea. Would it be possible if you could throw together a rough example using a flex tree control with your code?

Thanks a lot
0
 

Author Comment

by:toneDigital
Comment Utility
Hey

i have created this mxml page, however, the code you provided is not working. What am I doing wrong? Please help.
<?xml version="1.0"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

	 initialize="init()">

	 

    <mx:Script>

    

    	<![CDATA[

    	

    		import mx.collections.ArrayCollection;

    		

    		public function init():void {

    			var item:Object;

    			var array:Array = new Array();

    			

    			var xml:XML =      

			    <course>          

			        <section>              

			            <title>Introduction to Actionscript</title>              

			            <section>              

			                <title>Lesson 1: Variables</title>              

			                <section>                  

			                    <title>Topic 1: Data types</title>              

			                </section>              

			            </section>          

			        </section>      

			    </course>;    

			    

			    

			    

			    item = parseStructure(xml);  

			    

			    array.push(item);

			    

			    var arrColl:ArrayCollection = new ArrayCollection(array);

			    

			    Tree.dataProvider = arrColl;

			             

    		}

    		

   

			private function parseStructure(xml:XML):Object{    

			    var obj:Object = new Object();

			    obj.label = xml.title;

			    if(child.section != null) {

			        obj.children = new ArrayCollection();

			        for each (var child:XML in xml) {

			            obj.children.addItem(parseStructure(child));         

			        }

			    }

			    

			    return obj;

			    

			}



    	]]>

    	

    	

    </mx:Script>



    

    <mx:HBox>

        <mx:Tree id="Tree" labelField="title" 

            width="300"/>       

    </mx:HBox>

</mx:Application>

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 20

Accepted Solution

by:
ChristoferDutz earned 500 total points
Comment Utility
If you try this version it will work (The root element will be empty though)
<?xml version="1.0"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

				initialize="init()">

	

	<mx:Script>

    

		<![CDATA[

			

			import mx.collections.ArrayCollection;

			

			public function init():void {

				var item:Object;

				var array:Array = new Array();

				

				var xml:XML =      

					<course>          

						<section>              

							<title>Introduction to Actionscript</title>              

							<section>              

								<title>Lesson 1: Variables</title>              

								<section>                  

									<title>Topic 1: Data types</title>              

								</section>              

							</section>          

						</section>      

					</course>;    

				

				

				

				item = parseStructure(xml);  

				

				array.push(item);

				

				var arrColl:ArrayCollection = new ArrayCollection(array);

				

				Tree.dataProvider = arrColl;

				

			}

			

			

			private function parseStructure(xml:XML):Object{    

				var obj:Object = new Object();

				obj.label = xml.title;

				if(xml.section != null) {

					obj.children = new ArrayCollection();

					for each (var child:XML in xml.section) {

						obj.children.addItem(parseStructure(child));         

					}

				}

				

				return obj;

				

			}

			

		]]>

		

		

	</mx:Script>

	

	

	<mx:HBox>

		<mx:Tree id="Tree" width="300"/>       

	</mx:HBox>

</mx:Application>

Open in new window

0
 

Author Comment

by:toneDigital
Comment Utility
Thank you very much!
0
 

Author Comment

by:toneDigital
Comment Utility
Hey Chistofer,

How would I create leaf elements for items that don't require a directory? Currently all items in the tree are directories even if they are leaf elements. Thanks

tone
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now