PDA

View Full Version : Targeting XML Child


PM Hopper
07-17-2007, 12:17 PM
So I've got a projector file running locally that is using a hard-coded XML file that the client can update themselves. I know from the past that I'm doing this poorly however it's worked up until know?

This is the XML
<smartdata>
<splash_image>img/splash.png</splash_image>
<fade_splash>5</fade_splash>
<intro_video>video/intro.flv</intro_video>
<departments>10</departments>
<dep_data>data/dep_data</dep_data>
<department_names>
<department>Dep 1</department>
<department>Land Development</department>
<department>Human Resources</department>
<department>IT / IS</department>
<department>Land Development</department>
<department>Leasing</department>
<department>Acquisitions</department>
<department>Engineering</department>
<department>Construction</department>
<department>President</department>
</department_names>
<closing_video>video/trailer.flv</closing_video>
<closing_image>img/closing.png</closing_image>
</smartdata>

I can target all the main Children however when I get to "department_names" it only reads the first result?

This is my Actionscript.

trace(_level0.featXML.firstChild.childNodes[5].firstChild.childNodes[0].nodeValue);
trace(_level0.featXML.firstChild.childNodes[5].firstChild.childNodes[1].nodeValue);


The first result works fine however the second comes through undefined?

I've hard coded to get 15 results as I've built in enough however if I could figure this out I think a loop would work best?

Thanks

inhan
07-17-2007, 12:40 PM
Definitely. Better use a for loop:

var featXML:XML = new XML();
featXML.ignoreWhite = true;
featXML.onLoad = xmlDone;
featXML.load("feat.xml");

function xmlDone(ok) {
if (!ok) {
trace("xml not loaded");
return;
}
var node = this.firstChild.childNodes[5].childNodes;
for (var i:Number = 0; i < node.length; i++) {
trace(node[i].firstChild);
}
}
You should get rid of the second "firstChild"s in your trace btw.