{"id":2010,"date":"2004-03-21T11:12:03","date_gmt":"2004-03-21T11:12:03","guid":{"rendered":"http:\/\/www.soulhuntre.com\/items\/date\/2004\/03\/21\/an-xpath-out-of-the-darkness\/"},"modified":"2004-03-21T11:12:03","modified_gmt":"2004-03-21T11:12:03","slug":"an-xpath-out-of-the-darkness","status":"publish","type":"post","link":"http:\/\/legacyiamsenseiken.local\/2004\/03\/21\/an-xpath-out-of-the-darkness\/","title":{"rendered":"An (x)Path out of the darkness…"},"content":{"rendered":"

Read on and see the beauty that is a good [[XML]] parser in [[Flash]].<\/p>\n

Well, it goes to show you how things can change in just a few hours. Remember
\nmy Flash XML
\nparsing<\/a> issue? Well, I did some more poking around and I found out that the
\nfolks who built [[XML]] realized that getting at specific data in it sucks. They
\nbuilt something called [[XPath]] which you can sort of think of as a analog to
\n[[wp:SQL]] for a parsed XML tree. <\/p>\n

Anyway, below you will find a sample XML file, a snippet of code to parse
\nthat puppy and some reference links. If you have any questions [[let me know]].<\/p>\n

<\/p>\n

Sample XML input<\/b><\/u><\/p>\n

\n

<?xml version=”1.0″ ?>
<items>
    <item>
        <title>Welcome to the Tutorials<\/title>
        <weight>1<\/weight>
        <url>\/content\/sections\/tutorials\/article.php?article_id=16<\/url>
        <target>main_content<\/target>
    <\/item>
    <item>
        <title>article 15<\/title>
        <weight>25<\/weight>
        <url>\/content\/sections\/tutorials\/article.php?article_id=30<\/url>
        <target>main_content<\/target>
    <\/item>
<\/items><\/p>\n<\/blockquote>\n

A function to parse that XML tree<\/b><\/u><\/p>\n

\n

_global.parseMenu = function() {
<\/b>    var titlePath:String = “\/items\/item\/title”;
    var urlPath:String = “\/items\/item\/url”;
    var menuTitles = mx.xpath.XPathAPI.selectNodeList(_global.menuItems_xml.firstChild, “\/items\/item\/title”);
    var menuURLs = mx.xpath.XPathAPI.selectNodeList(_global.menuItems_xml.firstChild, “\/items\/item\/url”);
    var menuTargets = mx.xpath.XPathAPI.selectNodeList(_global.menuItems_xml.firstChild, “\/items\/item\/target”);
    var menuWeights = mx.xpath.XPathAPI.selectNodeList(_global.menuItems_xml.firstChild, “\/items\/item\/weight”);<\/p>\n

    for (var i = 0; i < menuTitles.length; i++) {
        \/\/ trace( “-_-_–_-_–_-_-> ” + i );
        _global.menuItems[i] = new Object;
        _global.menuItems[i].title = menuTitles[i].firstChild.nodeValue;
        _global.menuItems[i].url = menuURLs[i].firstChild.nodeValue;
        _global.menuItems[i].target = menuTargets[i].firstChild.nodeValue;
        _global.menuItems[i].weight = menuWeights[i].firstChild.nodeValue;<\/p>\n

        \/\/ trace(_global.menuItems[i].title);
        \/\/ trace(_global.menuItems[i].url);
        \/\/ trace(_global.menuItems[i].target);
        \/\/ trace(_global.menuItems[i].weight);
    }
}<\/p>\n<\/blockquote>\n

Reference links<\/b><\/u><\/p>\n