简体   繁体   中英

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());

您可以阅读我对AS3-最深的XML元素的回答通过使用SimpleXML,您可以以易于使用的方式解析XML。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM