如何循环遍历XML列表

时间:2011-09-07 07:55:26

标签: xml flex4

<fx:XML id="dataGridDataFieldsXML" source="assets/dataGridDataFieldsXML.xml"/>
<s:XMLListCollection id="showGridFields" source="{dataGridDataFieldsXML.viewer}"/>


I want to loop through my XML file and get the "field" value.I tried the following but nothing is getting through the trace statement:

var xmlTrack:XML;

                for each( xmlTrack in dataGridDataFieldsXML.fields_by_viewer.viewer )
                {
                    trace(xmlTrack.@field);
                }


My XML
<?xml version="1.0" encoding="UTF-8"?>

<fields_by_viewer>
    <viewer id="1" field="fieldName1"/>
    <viewer id="1" field="fieldName2"/>
</fields_by_viewer> 

Anything wrong here?

Thanks
flexyboz

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

   var myXML:XML = <users>
 <fname>Jane</fname>
 <fname>Susan</fname>
 <fname>John</fname>
</users>;

for each (var item in myXML.fname)
{
    trace(item);
}
/* output

更多信息请点击此处:http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_16.html

答案 1 :(得分:-1)

@Craig Mc..this works fine.

I also tried:
fieldsXMLListNew=new XMLListCollection(dataGridDataFieldsXML.viewer as XMLList);

And then looped and got my items using:

for each(var item in fieldsXMLListNew){
item.attribute("field");
}