开发者

Parsing AS3 XML objects

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/">
    <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?


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 :)


<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://localhost:49329/">
    <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());


You can read my answer to AS3 - Deepest XML Elements By using SimpleXML you can parse your XML in an easy to use manner.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜