# How do I loop through result objects in Flex?

I am having troubles by hand knotting via xml information that is obtained using an HTTPService call, the xml looks something similar to this:

<DataTable>
<Row>
<text>foo</text>
</Row>
<Row>
<text>bar</text>
</Row>
</DataTable>


When the webservice outcome occasion is discharged I do something similar to this:

for(var i:int=0;i&lt;event.result.DataTable.Row.length;i++)
{
if(event.result.DataTable.Row[i].text == "foo")
}


This code functions after that there is greater than 1 "Row" nodes returned. Nonetheless, it appears that if there is just one "Row" node after that the event.DataTable.Row object is not a mistake and also the code ultimately damages.

What is the correct means to loop via the HTTPService result object? Do I require to transform it to some sort of XMLList collection or an ArrayCollection ? I have actually attempted establishing the resultFormat to e4x which has yet to deal with the trouble

Thanks.

3
2022-06-07 14:37:40
Source Share

The trouble hinges on this declaration

event.result.DataTable.Row.length


length is not a building of XMLList, yet a method:

event.result.DataTable.Row.length()


it is complex, yet that is the means it is.

Enhancement: in fact, the most safe point to do is to constantly make use of a for each loop when repeating over XMLLists, in this way you never ever make the blunder, it is much less code, and also less complicated to read:

for each ( var node : XML in event.result.DataTable.Row )

4
2022-06-07 15:54:01
Source

Row isn't an array unless there are numerous Row components. It is aggravating. You need to do something similar to this, yet I have not created AS3 in a while so I neglect if there is an exists function.

if (exists(event.result.DataTable) && exists(event.result.DataTable.Row)){
if (exists(event.result.DataTable.Row.length)) {
for(var i:int=0;i<event.result.DataTable.Row.length;i++)
{
if (exists(event.result.DataTable.Row[i].text)
&& "foo" == event.result.DataTable.Row[i].text)
}
}
if (exists(event.result.DataTable.Row.text)
&& "foo" == event.result.DataTable.Row.text)

var returnedXml:Xml = new Xml(event.result.toString());