In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

notes on XML

Get Adobe Flash player
by wh0 03 Jul 2011
/**
 * Copyright wh0 ( http://wonderfl.net/user/wh0 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lFFR
 */

package {
    import com.actionscriptbible.Example;
    public class FlashTest extends Example {
        public function FlashTest() {
            // these little empty tags make an XMLList, not an XML object.
            var list:XMLList = <></>;
            trace(list.length());
            // you can populate XMLList like an array
            list[0] = <a/>;
            list[2] = <b/>;
            // it automatically squishes out any sparseness
            trace(list[1].toXMLString()); // <b/>
            
            // constructing XML(String) parses it instead of making a text node
            var foo:XML = new XML('<foo/>');
            trace(foo.toXMLString());
            // invalid syntax will cause an error
            var str:String = '1 < 2';
            try {
                new XML(str);
            } catch(e:Error) {
                trace(e);
            }
            // then how do you create an text node from a string?
            // escape it... AVM2 has "esc_xelem" op, but we don't have access to that
            var text:XML = new XML(str.replace(/</g, '&lt;'));
            trace(text);
            // something kludgy...
            var text2:XML = <>{str}</>[0];
            trace(text2);
            
            // what happens when you use E4X?
            var e4x:XML = <span><img /></span>;
            // bytecode is more like:
            // local = new XML('<span><img /></span>');
            // i.e., it's parsed at run time.
            trace(e4x.toXMLString());
            var fancy:XML = <div>{'before' + <test/> + 'after'}</div>;
            // bytecode is more like:
            // local = new XML('<div>' + __esc_xelem('before ' + new XML('<test/>') + ' after') + '</div>');
            // notice that this would implicitly call toString() on <test/>,
            // which would return nothing, since it is "simple"
            trace(fancy.toXMLString()); // nothing between before and after
            var fancier:XML = <div>{<test/>}</div>;
            trace(fancier.toXMLString()); // <div><test/></div>
            // note that now the argument is of type XML, which survives
            
            // what happens when you use filtering?
            var haystack:XMLList =
                <>
                    <i f="y"/>
                    <j f="n"/>
                    <k f="y"/>
                </>;
            var ik:XMLList = haystack.(@f == 'y');
            // bytecode is more like:
            // var _nodes:XMLList = haystack;
            // var _i:int = 0;
            // var _result:XMLList = new XMLList('');
            // while (__hasnext2(_nodes, _i)) { // this sets _i to the next key
            //     var _node = _nodes[_i];
            //     with (_node) {
            //         if (@f == 'y') {
            //             _result[_i] = _node;
            //         }
            //     }
            // }
            // ik = _result;
            trace(ik);
        }
    }
}