views:

213

answers:

2

I am getting an object back from ASP.NET, and when tracing the XML it as follows:

var xml:XML = new XML(event.message.body);
trace(xml);

I get the following output:

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://localhost:49329/"&gt;
    <string>One</string>
    <string>Two</string>
    <string>Three</string>
</ArrayOfString>

I am trying to iterate the structure to get the strings extracted, but to no avail. I am using the following code:

for each(var item:Object in xml.children())
{
    MonsterDebugger.trace(this, item.toString());
}

...and I know this is not entirely effective. It works now, but there is a way to get the strings with some syntax using the @ sign.

Can anyone please advise?

+2  A: 

You've pretty much got it. Should be able to write the same loop like so:

for each( var el:XML in xml.string ) {
  trace( el.toString() );
}

Take a look at these e4x docs, these are for Flex, but it all applies because it's just AS3. As you can see the state of documentation on a lot of this stuff is a little flaky :)

thenduks
That's exactly what's confusing me so much today, when I try this way, like I did most of today, I just get nothing at all?
Joe Zephyr
Ah, yea. Missed the namespace in the `ArrayOfString` :)
thenduks
+1  A: 
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://localhost:49329/"&gt;
    <string>One</string>
    <string>Two</string>
    <string>Three</string>
</ArrayOfString>

You have to take care of the Namespace too:

var ns:Namespace = new Namespace("http://localhost:49329/");
var strings:XMLList = xml.ns::string;
for each(var str:XML in strings)
  trace(str.text());
Amarghosh
I thought so, darn!!! It wasted about 2 hours of my day. Thanks for the reply!!!
Joe Zephyr
just 2 hours? I've wasted more than a day pulling my hair over namespaces ;)
Amarghosh
Hehehe! Respect man! You have my most sincere sympathy. :)
Joe Zephyr