PDA

View Full Version : XML to array problems. Help!



iselsewhere
January 12th, 2005, 12:32 PM
I'm a newbie at XML and I'm trying to make my portfolio XML-driven. What I'm trying to do is similiar to a photogallery. The difference is that I want "sub pictures" to every item in the gallery. Example: If I click on a project name in the portfolio an image should appear. Then I want to be able to browse between different images within that project. When I click on a another project a different set of pictures will be loaded.

My XML file looks as exemplified below. What I'm having troubles figuring out how to do is getting the array right.

I want to access the data something like this:

imageArray[0].path[1] would return "http://www.pic.com/picA1.jpg"
imageArray[0].title returns "Title A"

I fail to parse the XML into an multidimensional array like this.. Please help me to do this in the easiest way!




<images>
<item title="Title A">
<pic path="http://www.pic.com/picA1.jpg">

<desc>First A picture</desc>
</pic>

<pic path="http://www.pic.com/picA1.jpg">
<desc>Second A picture</desc>
</pic>

</item>




<item title="Title B">
<pic path="http://www.pic.com/picB1.jpg">

<desc>First B picture</desc>
</pic>

<pic path="http://www.pic.com/picB2.jpg">
<desc>Second B picture</desc>
</pic>

</item>

...and so on

charlie_su1986
January 12th, 2005, 08:26 PM
Well i can't do exactly what you want, but it's pretty close.
I use " pic[0][1].attributes.path " instead of " imageArray[0].path[1] "


myXml = new XML();

myXml.ignoreWhite = true;
myXml.onLoad = function(success) {
if (success) {
item = this.firstChild.childNodes;
pic = new Array();
for (i=0; i<item.length; i++) {
pic[i] = new Array();
pic[i] = item[i].childNodes;
}
trace(pic[0][0].attributes.path)
//will print http://www.pic.com/picA1.jpg
trace(pic[1][0].attributes.path)
//will print http://www.pic.com/picB1.jpg
trace(item[0].attributes.title)
//will print Title A
trace(item[1].attributes.title)
//will print Title B
} else {
trace("error loading xml");
}
}
myXml.load("myXml.xml");


Hope what i have up there can give you some ideas :)

iselsewhere
January 14th, 2005, 05:43 AM
Thank you! This is very useful. I think it will do what I want.