[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Recursively convert XML into multidimensional array

Posted on 2010-09-17
6
Medium Priority
?
617 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33702185
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
ID: 33702515
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
ID: 33704878
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 20

Accepted Solution

by:
ChristoferDutz earned 2000 total points
ID: 33705454
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
ID: 33709499
Thank you very much!
0
 

Author Comment

by:toneDigital
ID: 33737574
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

650 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