• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3517
  • Last Modified:

firstChild, childNodes - I need an explanation

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
lookouts
Asked:
lookouts
  • 5
  • 4
1 Solution
 
blue-genieCommented:
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
 
lookoutsAuthor Commented:
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
 
lookoutsAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
blue-genieCommented:
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
 
lookoutsAuthor Commented:
Thank you!
0
 
lookoutsAuthor Commented:
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
 
blue-genieCommented:
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
 
blue-genieCommented:
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
 
lookoutsAuthor Commented:
Awesome, thank you again!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now