Skip to main content
Inspiring
February 16, 2008
Answered

Attempting to learn XML all over again...

  • February 16, 2008
  • 4 replies
  • 478 views
Okay, I’ve been at this for a good few hours now, but no matter how much I read the help, it doesn’t seem to want to work.

I have some xml that I’ve managed to read in and am able to properly trace:

<fruitBowl>
<fruit name=”apples” colour=”red” seeds=”yes” />
<fruit name=”pears” colour=”green” seeds=”yes” />
<fruit name=”grapes” colour=”purple” seeds=”sometimes” />
<fruit name=”blueberries” colour=”blue” seeds=”no” />
</fruitBowl>

Now, I want to be able to get the first fruit (and the next after, etc), and its attributes.
Logically, I think I should be able to do this by going (or something similar):

xmlListObject.child(0).attribute(“name”)

But actionscript 3 seems to disagree. When I trace the above (and tracing "xmlList.Object.child(0)" ), I get a blank (which appears to be because the tag actually has no value).

If I trace xmlListObject itself, I get my whole xml. If I trace xmlListObject.length(), it does return the right amount of children. But I can’t seem to isolate any 1 child and get an attribute from it.

Any ideas?
This topic has been closed for replies.
Correct answer kglad
what do mean by, you finally got it? you had it in your first post. you just needed to correct your malformed xml file.

4 replies

kglad
Community Expert
Community Expert
February 17, 2008
you're welcome.
kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
February 17, 2008
what do mean by, you finally got it? you had it in your first post. you just needed to correct your malformed xml file.
Some1WonAuthor
Inspiring
February 17, 2008
Hi kglad,

Hm, you know what? You’re right. Going back to my xml reading for the 3rd time I see what I had in my first post does actually work...

It was something else in my logic, it seemed I was accidentally trying to get the child of a child the first time, which didn’t exist (which was why it was blank every time). Reading my ‘answer’ again, it seemed I had unknowingly fixed that and thought it was the real ‘answer’. =b But now I see the root of the problem.

Thanks kglad for the insight.
Some1WonAuthor
Inspiring
February 17, 2008
Ah, I’ve finally gotten it:

<fruitBowl>
<fruit name=”apples” colour=”red” seeds=”yes” />
<fruit name=”pears” colour=”green” seeds=”yes” />
<fruit name=”grapes” colour=”purple” seeds=”sometimes” />
<fruit name=”blueberries” colour=”blue” seeds=”no” />
</fruitBowl>

From the xmlList that contains all of this, I first need to access its children (all the fruit nodes).

xmlObject.children() will give me:

<fruit name=”apples” colour=”red” seeds=”yes” />
<fruit name=”pears” colour=”green” seeds=”yes” />
<fruit name=”grapes” colour=”purple” seeds=”sometimes” />
<fruit name=”blueberries” colour=”blue” seeds=”no” />

Then to get the first element I can go:

xmlObject.children()[0] (like in an array)

<fruit name=”apples” colour=”red” seeds=”yes” />

Finally to get an attribute ‘colour’:

xmlObject.children()[0].attribute(“colour”);

If there are better/other ways to do this, then feel free to comment.
kglad
Community Expert
Community Expert
February 16, 2008
you have a malformed xml file. there should be an equal sign after colour in blueberries.
Some1WonAuthor
Inspiring
February 17, 2008
Sorry, that was a typo. I’ve fixed it, but my xml does validate in the first place.