Solved

firstChild, childNodes - I need an explanation

Posted on 2008-10-17
9
3,445 Views
Last Modified: 2013-11-12
I understand Actionscript 2.0, Flash, and how XML works.

I don't understand how to pull information from an XML document correctly.

I'm confused about what firstChild and childNodes mean. I have some understanding of the attributes keyword using Actionscript, as well as nodeValue and nodeName. I understanding that childNodes is an array and can refer to children in an XML doc, but that's where I'm confused.

I don't know how to structure my code properly to pull/parse XML data.

Attached is a snippet of my XML doc.

I also attached what I think is how Flash reads my XML doc with firstChild, childNodes, etc.


<?xml version="1.0"?>
 

<flashcards>

	<fronts>

		<card1_front height="100" y="-20">Turner Field</card1_front>

		<card2_front height="120" y="-40">Pro Player Stadium</card1_front>

		<card3_front height="120" y="-40">Comisky Park</card3_front>

	</fronts>

	<backs>

		<card1_back height="120" y="-40">Braves</card1_back>

		<card2_back height="120" y="-40">Marlins</card2_back>

		<card3_back height="120" y="-40">White Sox</card3_back>

	</backs>

	<main>

		<main_text>Welcome to the flashcards example.</main_text>

		<instructions_text>Click each Flashcard to proceed through the exercise.</instructions_text>

	</main>

</flashcards>

Open in new window

xml.gif
0
Comment
Question by:lookouts
  • 5
  • 4
9 Comments
 
LVL 39

Expert Comment

by:blue-genie
ID: 22742593
i'll have a go

so in AS2.
firstChild will return <flashCards>
if you wanted to access <fronts> firstChild.childNodes[0] would do it. as would childNodes[0].childNodes[0];
and so you'd go on to traverse the tree.


however, the firstNode and childNode properties aren't used in AS3 with the XML object anymore. The XMLNode object is now what we new before as the AS2 XML object, so if you turn your XML object into XMLNode you can still access the firstNode and childNode properties.

hope that helps
0
 

Author Comment

by:lookouts
ID: 22742935
Thanks, blue-genie - I don't know why it's so hard for me to understand. How do I access the rest? I'm still stumped, and when I'm writing code I try all kinds of possibilities (going from firstChild.childNodes[0] to firstChild.firstChild.firstChild.childNodes[1] and many other combinations until I get it right). And I'm glad it changed in AS 3 because it's too confusing. I can't use AS3 because our clients won't update their Flash player to 9 or 10.

BUT - Can someone post how to access each node that I listed, in Actionscript format?


0
 

Author Comment

by:lookouts
ID: 22744282
To get to the 3rd child inside "fronts", I tried trace(this.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue); to no avail. For some reason, tracing 0 and 1 works on the second childNodes property, but not for the third. I was expecting it to trace out "Comisky Park". What gives? I'm at my wit's end. I've tried tons of tutorials, all of them assume it's easy as pie to traverse an XML document. :(
0
 
LVL 39

Accepted Solution

by:
blue-genie earned 500 total points
ID: 22747729
no think about it this way.
firstChild is flashCards yeah - which includes all the nodes in there.
so the firstChild or childNodes[0] (which are the same thing) of flashCards would be fonts - agree?
so then we have firstChilds.childNodes[0] gives you the first fronts node yeah.
so if you were to do a firstChild.childNodes[0].length - it would tell you 3 because there's 3 card nodes inside that first front node.
so it order to access cominsky park...
myXML.firstChild.childNodes[0].childNodes[2].firstChild

this would also work

myXML.firstChild.childNodes[0].childNodes[2].childNodes[0]

lately i've found that using nodeValue doesn't always work. to access the value, treat it as a child of the parent node.

oh take note that your xml is screwy here
>>
<card2_front height="120" y="-40">Pro Player Stadium</card1_front>
closing tag is wrog.

make sense?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Closing Comment

by:lookouts
ID: 31507172
Thank you!
0
 

Author Comment

by:lookouts
ID: 22758851
What I still don't understand is how to get to the xml part of <main> - I tried trace(this.firstChild.firstChild.firstChild.childNodes[0].childNodes[0].firstChild); and got undefined. How come? Seems like if fronts is also a firstChild, then firstChild.firstChild would get to the backs and firstChild.firstChild.firstChild would get to main. Frustrating! I'm glad they changed it in AS3.
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 22759221
if it helps i still get the odd undefined and have to try again :-)

ok so lets look at accessing the <main> node.

you tried
trace(this.firstChild.firstChild.firstChild.childNodes[0].childNodes[0].firstChild);

remember that it always refers back to the first node that you're referencing,
so this refers to the xml object, no problem.
firstChild of the xmlObject is <flashCards>
this.firstChild = <flashCards>
this.firstChild.firstChild = <fronts>
this.firstChild.firstChild.firstChild = <card1_front>
this.firstChild.firstChild.firstChild.childNodes[0] = Turner Field //because that's the child node of card1_front
this.firstChild.firstChild.firstChild.childNodes[0] .childNodes[0] //undefined Turner Field doesn't have children
this.firstChild.firstChild.firstChild.childNodes[0].childNodes[0].firstChild //undefined definately doesn't have grandchildren.

in order to access the main node, if you look at the firstChild - it has 3 children
remember xml nodes is zero based as per arrays.

so if you simply say

this.firstChild.childNodes[2]); //that will return the entire main node as you're saying give me the 3rd child of my first node.
this would return the same

trace(this.firstChild.firstChild.nextSibling.nextSibling);

firstChild of my firstChild is <fronts> - his next sibling (i.e. on same level) is backs, and backs next sibling is main.

does that help?
0
 
LVL 39

Expert Comment

by:blue-genie
ID: 22759273
oh and subsequently


this.firstChild.firstChild.nextSibling.nextSibling.childNodes[0] will return
<main_text>Welcome to the flashcards example.</main_text>

and the child of that
trace(this.firstChild.firstChild.nextSibling.nextSibling.childNodes[0].childNodes[0]);
will return
Welcome to the flashcards example.
0
 

Author Comment

by:lookouts
ID: 22777956
Awesome, thank you again!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mongodb hierarchy 2 47
Can't play streaming video on event.on24.com 7 198
Specific format 21 181
Formatting issues in XSL FO 3 30
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…
Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
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.

910 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

28 Experts available now in Live!

Get 1:1 Help Now