PDA

View Full Version : Help with XML Loop


adydas
11-26-2006, 09:53 AM
I'm still new to parsing XML and I'm having trouble assigning attribute values into an array. Here is my xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<news>

<press date="October 28" title="1">
<short><![CDATA[1]]>
</short>
<long><![CDATA[2]]>
</long>
<links>
<link src="http://www.test.com" name="test1a" />
<link src="http://www.test.com" name="test1b" />
<link src="http://www.test.com" name="test1c" />
<link src="http://www.test.com" name="test1d" />
</links>
</press>

<press date="October 27" title="2">
<short><![CDATA[1]]>
</short>
<long><![CDATA[2]]>
</long>
<links>
<link src="http://www.test.com" name="test21" />
<link src="http://www.test.com" name="test2b" />
<link src="http://www.test.com" name="test2c" />

</links>
</press>
</news>


And here is my code var data_array:Array;
links_array = new Array();
name_array = new Array();

var main_xml:XML = new XML();
main_xml.ignoreWhite = true;
main_xml.load("news.xml");


main_xml.onLoad = function(success:Boolean) {

if (success) {
write_xml();
} else {

trace("fail");
}
};


function write_xml() {

data_array = main_xml.firstChild.childNodes;

for(i=0; i< data_array.length; i++) {
links_array[i] = main_xml.firstChild.childNodes[i].childNodes[2].childNodes;

for(n=0;n<links_array[i].length; n++) {
name_array[n]=links_array[i][n].attributes.name
trace(links_array[i][n].attributes.name);
}

}

trace(name_array);

}


What I am trying to achieve here is put all my links and urls into separate arrays that I can loop through and assign to a text field for each of the parent press items. My trace outputs correctly but I can't seem to get the name_array populated correctly. Would be nice to be able to call something like name_array[0][2] and get "test1b" back. Any help or direction is appreciated.