To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > General Help > XML

Reply
 
Thread Tools Display Modes
Old 07-21-2007, 08:01 PM   #1
SilverVenom
Senior Member
 
Join Date: Jun 2006
Posts: 100
[RESOLVED] XML Tree question

Hi, I had a question which relates to loading a complex XML file into flash.

Lets say I had an XML file which looked like this:

PHP Code:
<data>
    <
flower>
        <
info name="blah" id="001" desc="/gfx/blah.png" />
        <
info name="blah2" id="002" desc="/gfx/blah2.png" />
    </
flower>
    
    <
animal>
        <
info name="dog" id="046" desc="/gfx/dog.png" />
    </
animal>
</
data>
How would I specify the level which contains the "blah2" info?
How would I specifiy the level which contains the "dog" info?

What I mean by this is do I say "this.firstChild.childNodes" or... this is the part I am confused by. I do not understand how to tell flash where to look for info when it loads the xml file.

Let me know if you need clarification. Thanks in advance, - Peter
SilverVenom is offline   Reply With Quote
Old 07-21-2007, 09:02 PM   #2
renderhjs
Student
 
Join Date: Apr 2001
Location: -
Posts: 4,756
lets say your xml file is called "xml_file.xml",

this code on the first frame of the flash movie wich is saved in the very same folder as your xml file:

PHP Code:
xml = new XML();
xml.ignoreWhite = true;
xml.onLoad = xmlInit;

xmlInit = function(ok){
    if (
ok){
        
init();
    }else{     
        
trace("no such XML file");
    }
}
xml.load("xml_file.xml");


function
init(){
    var
a = xml.firstChild.childNodes[0]childNodes[1].attributes.name;
    
trace(" your first question :"+a);

    var
b = xml.firstChild.childNodes[1]childNodes[0].attributes.name;
    
trace(" your second question :"+b);
    
//------

    
var c = xml.firstChild.firstChild.firstChild.nextSibling.attributes.name;
    
trace("first question alternative way :"+c);

    var
d = xml.firstChild.firstChild.nextSibling.firstChild.attributes.name;
    
trace("second question alternative way :"+d);
}

maybe this post from today helps as well:
http://board.flashkit.com/board/showthread.php?t=737052
renderhjs is offline   Reply With Quote
Old 07-21-2007, 09:12 PM   #3
SilverVenom
Senior Member
 
Join Date: Jun 2006
Posts: 100
Many thanks, that was exactly what I was looking for.
SilverVenom is offline   Reply With Quote
Old 07-21-2007, 09:17 PM   #4
renderhjs
Student
 
Join Date: Apr 2001
Location: -
Posts: 4,756
some explaination the variables I wrote:

a.)
- first you call the xml object in this case "xml"
- because each XML file has to start with only 1 node you have to enter that one (the <data> one) with ".firstChild"
- now you are in the first Child <data> to gain access to the child nodes it´s the easiest way to convert the childNodes to an array for faster handling using "childNodes"
- what follows are the brackets [0] and the Child number (an array starts always with 0 so the 1st child would be 0 and the last one last-1) ,- so the code ".childNodes[0]" tells AS to take the first CHild from the childNodes of the node that stands before this part.
- because you have another hierarchy (thus gooing deeper) we need to repeat the last 2 steps again to go deeper into the hierarchy but in this case we need the 2nd child of those childNodes (remember first = 0, 2nd = 1,...) thus the code is "childNodes[1]."
- to finally read out the attribute of that node use the command "attributes." followed by your attribute name from the xml file in your case "name" but it could be as well "desc" or "id"

b.) follows the same pattern as a.)

c.) this is an alternative way without using the childNodes command (wich converts the nodes to an easy array object)- usually this results in very big code but it is also possible doing it this way.

- call the xml object "xml"
- goto the first Child <data> using "firstChild."
- from that node go again into the first Child wich would be <flower> using "firstChild."
- again go into the first child even though we need the 2nd child and not the first child. This is because we need to enter that hierarchy anyway before we can switch to another sibling- so firstChild is also used to enter nodes.
- since we have the 1st child and not the 2nd one use now "nextSibling" to go to the next Sibing (2nd node where we want to go)
- call the method "attributes" followed by your attribute name "name" to access your variable

d.) follows same pattern as c.)
renderhjs is offline   Reply With Quote
Old 07-22-2007, 08:52 AM   #5
SilverVenom
Senior Member
 
Join Date: Jun 2006
Posts: 100
thanks for the in depth, but the only part I was confused with was the " xml.firstChild.childNodes[0]childNodes[1].attributes.name;" part.


My final code ended up as:

code:

function getData() {
var x = new XML();
x.ignoreWhite = true;

var Name:Array = new Array();
var id:Array = new Array();
var url:Array = new Array();

x.onLoad = function(){
var Data:Array = this.firstChild.childNodes[_root.accordion.selectedIndex].childNodes;
for(i=0;i<Data.length;i++) {
Name.push(Data[i].attributes.name);
id.push(Data[i].attributes.id);
url.push(Data[i].attributes.url);
list.addItem({label:Name[i], data:id[i]});
window.contentPath:url[i];
}
}
x.load("data.xml");
}



There was a minor change to the XML. Where "desc" was became "url".

Last edited by SilverVenom; 07-22-2007 at 08:55 AM.
SilverVenom is offline   Reply With Quote
Old 07-27-2007, 11:37 AM   #6
Alluvian
Senior Member
 
Alluvian's Avatar
 
Join Date: Jun 2006
Posts: 967
*snip*


Sorry, I decided to post my question as a new thread instead of burying it in a [resolved] thread.

Last edited by Alluvian; 07-27-2007 at 11:53 AM.
Alluvian is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > General Help > XML

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:31 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.