all groups > flash data integration > september 2006 >
You're in the

flash data integration

group:

pulling in children of children in XML


pulling in children of children in XML flipflop9
9/23/2006 6:23:09 PM
flash data integration: Hey all-

Newbie to XML here and I have what I *hope* is an easy problem, but I just
don't know the answer. And as I have to try and show this by Monday, I'm sort
of stuck.

I am trying to pull in information about houses via an xml file. Each house
has 1 address, 1 owner, 1 description, and about 6 photos. I have no problem
accessing the address, owner, etc, and I can always access the first photo, but
whenever I cycle through the photos, I seem to cycle through the first photo
for every house, not every photo for each house.

Basically, I need the children <images> to go up by one, without the the
parent going up by 1. I am hoping I am describing this well.

My code looks like this:

-----
total = xmlNode.childNodes.length;
totalb = xmlNode.childNodes[0].childNodes[5].childNodes.length;
totalc = (totalb-1);
for (i=0; i<total; i++) {
thumb[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
address[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
construct[i] = xmlNode.childNodes[i].childNodes[2].firstChild.nodeValue;
work[i] = xmlNode.childNodes[i].childNodes[3].firstChild.nodeValue;
description[i] = xmlNode.childNodes[i].childNodes[4].firstChild.nodeValue;
image[i] = xmlNode.childNodes[i].childNodes[5].firstChild.firstChild.nodeValue;
}


where total = number of houses
and totalb = number of photos for that house.

This works fine - it is pulling in the accurate number of both houses and
photos.

---
My XML looks like this:

<projects>
<pic>
<thumb>boulevard_staircase1_thumb.jpg</thumb>
<address>Jim's Place</address>
<construct>July 28, 2006</construct>
<work>Framing, Electrical, Flooring, Roofing</work>
<description>This is Jim's place</description>
<photos>
<image>jim1.jpg</image>
<image>jim2.jpg</image>
<image>jim3.jpg</image>
<image>4.jpg</image>
<image>jim5.jpg</image>
</photos>
</pic>

<pic>
<thumb>boulevard_staircase1_thumbb.jpg</thumb>
<address>Bob's Place</address>
<construct>January 20, 2006</construct>
<work>Bob's Stuff</work>
<description>Isn't Bob Great?</description>
<photos>
<image>bobs1.jpg</image>
<image>bobs2.jpg</image>
<image>bobs3.jpg</image>
<image>bobs4.jpg</image>
</photos>
</pic>

---

<image> is the tag I am having problem with.


THANKS a lot, and a frosty beer to whoever can help!

Noah
Re: pulling in children of children in XML flipflop9
9/23/2006 6:33:27 PM
Me again - to sum up - the problem code is this:

image[i] = xmlNode.childNodes[i].childNodes[5].firstChild.firstChild.nodeValue;

This always shows me the first value of the <images> tag in every project.
What I want is to do, is set a variable p, where the image I see is image
number p (from first-sixth), all in the *same* project.

Thanks,
Noah
Re: pulling in children of children in XML The Feldkircher
9/26/2006 12:00:00 AM
I suggest you load your XML nodes in to an Array for each nodevalue
then you can cycle thru the array for each pic rather than trying to extract
each childnode value.

A simple way to do this is using XPath statements to load Arrays

Try this

import mx.xpath.XPathAPI;


var projectxml:XML = new XML();
projectxml.ignoreWhite = true;
projectxml.onLoad = function(success:Boolean) {
trace("onload...");
if (success) {
trace("success...");
var thePath_str:String = "projects/pic/photos";
var pic_array:Array = XPathAPI.selectNodeList(this.firstChild, thePath_str);

// then you can use a loop to step thru the images

for (var i:Number = 0; i < title_array.length; i++) {
trace(pic_array[i].firstChild.nodeValue);
}
}

projectxml.load("project.xml");

You can extend this to load all your node values into arrays then you can step
thru the data far more easily.

Re: pulling in children of children in XML flipflop9
9/26/2006 9:31:43 PM
The Feldkircher -

Re: pulling in children of children in XML The Feldkircher
9/27/2006 12:00:00 AM
Yo Bud

Just noticed bad info,

should read

for (var i:Number = 0; i < pic_array.length; i++){

not

AddThis Social Bookmark Button