lookouts
asked on
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.
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>
xml.gif
ASKER
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.firs tChild.chi ldNodes[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?
BUT - Can someone post how to access each node that I listed, in Actionscript format?
ASKER
To get to the 3rd child inside "fronts", I tried trace(this.firstChild.chil dNodes[0]. childNodes [2].firstC hild.nodeV alue); 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. :(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!
ASKER
What I still don't understand is how to get to the xml part of <main> - I tried trace(this.firstChild.firs tChild.fir stChild.ch ildNodes[0 ].childNod es[0].firs tChild); and got undefined. How come? Seems like if fronts is also a firstChild, then firstChild.firstChild would get to the backs and firstChild.firstChild.firs tChild would get to main. Frustrating! I'm glad they changed it in AS3.
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.firs tChild.fir stChild.ch ildNodes[0 ].childNod es[0].firs tChild);
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 .firstChil d = <card1_front>
this.firstChild.firstChild .firstChil d.childNod es[0] = Turner Field //because that's the child node of card1_front
this.firstChild.firstChild .firstChil d.childNod es[0] .childNodes[0] //undefined Turner Field doesn't have children
this.firstChild.firstChild .firstChil d.childNod es[0].chil dNodes[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.firs tChild.nex tSibling.n extSibling );
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?
ok so lets look at accessing the <main> node.
you tried
trace(this.firstChild.firs
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
this.firstChild.firstChild
this.firstChild.firstChild
this.firstChild.firstChild
this.firstChild.firstChild
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
this would return the same
trace(this.firstChild.firs
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?
oh and subsequently
this.firstChild.firstChild .nextSibli ng.nextSib ling.child Nodes[0] will return
<main_text>Welcome to the flashcards example.</main_text>
and the child of that
trace(this.firstChild.firs tChild.nex tSibling.n extSibling .childNode s[0].child Nodes[0]);
will return
Welcome to the flashcards example.
this.firstChild.firstChild
<main_text>Welcome to the flashcards example.</main_text>
and the child of that
trace(this.firstChild.firs
will return
Welcome to the flashcards example.
ASKER
Awesome, thank you again!
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