<p>I am having problems manually looping through xml data that is received via an HTTPService call, the xml looks something like this: </p>

<pre><code>&lt;DataTable&gt;
    &lt;Row&gt;
        &lt;text&gt;foo&lt;/text&gt;
    &lt;/Row&gt;
    &lt;Row&gt;
        &lt;text&gt;bar&lt;/text&gt;
    &lt;/Row&gt;
&lt;/DataTable&gt;
</code></pre>

<p>When the webservice result event is fired I do something like this:</p>

<pre><code>for(var i:int=0;i&amp;lt;event.result.DataTable.Row.length;i++)
{
    if(event.result.DataTable.Row[i].text == "foo")
        mx.controls.Alert.show('foo found!');
}
</code></pre>

<p>This code works then there is more than 1 "Row" nodes returned. However, it seems that if there is only one "Row" node then the <em>event.DataTable.Row</em> object is not an error and the code subsequently breaks. </p>

<p>What is the proper way to loop through the <em>HTTPService</em> result object? Do I need to convert it to some type of <em>XMLList</em> collection or an <em>ArrayCollection</em>? I have tried setting the resultFormat to <em>e4x</em> and that has yet to fix the problem...</p>

<p>Thanks.</p>

[{'answer_id': 40930, 'author': 'dlamblin', 'author_id': 459, 'author_profile': 'https://Stackoverflow.com/users/459', 'pm_score': 2, 'selected': False, 'text': '<p>Row isn\'t an array unless there are multiple Row elements. It is annoying. You have to do something like this, but I haven\'t written AS3 in a while so I forget if there\'s an exists function.</p>\n\n<pre><code>if (exists(event.result.DataTable) &amp;&amp; exists(event.result.DataTable.Row)){\n  if (exists(event.result.DataTable.Row.length)) {\n    for(var i:int=0;i&lt;event.result.DataTable.Row.length;i++)\n    {\n        if (exists(event.result.DataTable.Row[i].text)\n        &amp;&amp; "foo" == event.result.DataTable.Row[i].text)\n            mx.controls.Alert.show(\'foo found!\');\n    }\n  }\n  if (exists(event.result.DataTable.Row.text)\n  &amp;&amp; "foo" == event.result.DataTable.Row.text)\n      mx.controls.Alert.show(\'foo found!\');\n}\n</code></pre>\n'}, {'answer_id': 40937, 'author': 'Shawn', 'author_id': 26, 'author_profile': 'https://Stackoverflow.com/users/26', 'pm_score': 1, 'selected': False, 'text': '<p>I would store it in an Xml object and then use its methods to search for the node value you need.</p>\n\n<pre><code>var returnedXml:Xml = new Xml(event.result.toString());\n</code></pre>\n'}, {'answer_id': 41222, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 3, 'selected': True, 'text': "<p>The problem lies in this statement</p>\n\n<pre><code>event.result.DataTable.Row.length\n</code></pre>\n\n<p><code>length</code> is not a property of <code>XMLList</code>, but a method:</p>\n\n<pre><code>event.result.DataTable.Row.length()\n</code></pre>\n\n<p>it's confusing, but that's the way it is.</p>\n\n<p><em>Addition:</em> actually, the safest thing to do is to always use a <code>for each</code> loop when iterating over <code>XMLList</code>s, that way you never make the mistake, it's less code, and easier to read:</p>\n\n<pre><code>for each ( var node : XML in event.result.DataTable.Row )\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/40913', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1638/']