views:

30

answers:

2

I'm trying to parse some xml in AS3, but the nodes I'm trying to call up are named keywords like 'name' and 'object'. Can I escape these words somehow?

A: 

How about...

var xml:XML =
    <xml>
        <name>Name 1</name>
        <name>Name 2</name>
        <object>Object 1</object>
        <object>Object 2</object>
    </xml>;

trace(xml.name);
trace(xml.object);
//They are both XMLLists.

Just use regular E4X syntax. More about it here.

MrKishi
A: 

As MrKishi already pointed out, these keywords are not always a problem, so you can use regular syntax.

There some cases, though, were valid xml tag and attribute names are not valid actionscript (off the top of my head, this happens when you have hyphens in your xml structure).

A workaround is using square brackets and/or more verbose method calls.

var xml:XML =
    <xml>
        <node-with-hyphens attr-with-hyphens="123">Object 1</node-with-hyphens>
    </xml>;

trace(xml["node-with-hyphens"]);
trace(xml.child("node-with-hyphens"));
trace(xml["node-with-hyphens"].attribute("attr-with-hyphens"));
Juan Pablo Califano