Solved

firstChild, childNodes - I need an explanation

Posted on 2008-10-17
9
3,442 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Header in SQL XML PATH 4 46
YouTube Friendly Video Addresses 3 77
Adobe Indesign CS6 4 78
microsoft access - xml 10 29
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

747 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

13 Experts available now in Live!

Get 1:1 Help Now