Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

firstChild, childNodes - I need an explanation

Posted on 2008-10-17
9
Medium Priority
?
3,497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?

 
LVL 39

Accepted Solution

by:
blue-genie earned 2000 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
 

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

661 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