Search code examples
xmlflashactionscriptactionscript-2

Search box inside Flash, read data from local xml (actionscript)


I have trouble making this work. I try to make a display data on flash from external xml. So far it works just getting first child, but I need a search box in a large xml data. Is any way to search through custom nodes? Let’s say on input text I write “june_123” so after click button I get the values (name, score, level, lives) and display it.

Code xml

<mark56 val1="5000" val2="23" Val3="3"/>
<june_123 val1="63400" val2="45" Val3="2"/>
<uglyman val1="600" val2="12" Val3="1"/>
<sugarpunch val1="456223" val2="54" Val3="3"/>

Code ActionScript

//Frame 1

getxmldata = new XML();
getxmldata.load("xmldata.xml", "");

//Frame 2

stop();

//Button 1

on (release)
{
    if (getxmldata.loaded) 
    {
        var innerdata = getxmldata.firstchild;
        data1 = inner.nodeName;
        data2 = inner.attributes.val1;
        data3 = inner.attributes.val2;
        data4 = inner.attributes.val2;
    }
}

Then I have in flash 4 dynamic text box with variable “data1”, “data2”, “data3”, ”data4”. And one input text box whit the variable name “finder” I need to find inside xml the node name given by the input text. Or is any way to do. If a change xml for txt or something.


Solution

  • I wrote a code that work very well with your xml file. I used a btn_search button and txt_search text field that I inserted in my main stage. Firstly we load our xml file, then we type a player name in our txt_search and then run the search with btn_search:

    var xml:XML = new XML()
        xml.load('xmldata.xml') 
    
    btn_search.onRelease = function(){
    
        if(txt_search.text != ''){
            search_player_by_name(txt_search.text)
        }
    
    }
    
    function search_player_by_name(player_name){    
    
        if(!xml.loaded){
            trace('the xml file is not yet loaded')
            return false
        }
    
        var players, player
    
        players = xml.childNodes
        for (i=0; i<players.length; i++) {
            player = players[i]
            if(player.nodeName != '' && player.nodeName != null){
                if(player.nodeName == player_name){
                    trace('name : ' + player.nodeName)
                    trace('score : ' + player.attributes.val1)
                    trace('level : ' + player.attributes.val2)
                    trace('lives : ' + player.attributes.Val3)
                    // return an array that contains player data
                    return new Array(player.nodeName, player.attributes.val1, player.attributes.val2, player.attributes.Val3)
                }
            }
        }
        trace('there is no player with that name : ' + player_name)
        return false
    
    }