SoFunction
Updated on 2025-04-13

A relatively complete and concise tutorial on processing XML document data in Flash. Previous article


Open Flash and create a new Flash document, save it to the directory of the xml document just now, and name it. Enter the following code in the first frame:

Copy the codeThe code is as follows:

//. 
//Instantiate an XML object.
var myxml:XML = new XML(); 
//Ignore the spaces in the xml document during analysis.
 = true; 
//Load the document.
(""); 
//Call event.
 = function(success:Boolean) 

//If the loading is successful, success=true; otherwise success=false;
    if (success) { 
trace("Loading successfully!");
//Output the node name of the top-level node and the value of the attribute name in the top-level node.
        trace(+":"+); 
//Use an array to refer to the array of child nodes in the top-level node.
        var child_arr:Array = ; 
//Use nested for statements to traverse all data in the xml document.
//This for traverses child nodes under top-level nodes.
        for (var i = 0; i<child_arr.length; i++) { 
//Output the node name of the child node under the top-level node and the value of the attribute name in the child node under the top-level node.
            trace(child_arr[i].nodeName+":"+child_arr[i].); 
//This for traverses the child nodes under the child nodes under the top-level node.
            for (var j = 0; j<child_arr[i].; j++) { 
//Output the node name of the child node under the child node under the top-level node and the value of the attribute name in the child node under the child node under the top-level node. Don't be dizzy. Look at the output panel and you will understand the relationship between them.
                trace(child_arr[i].childNodes[j].nodeName+":"+child_arr[i].childNodes[j].); 
            } 
        } 
    } else { 
trace("Loading failed!");
    } 
}; 
Experiment 3:
Use XML document data to log in.
Create a new XML document, enter the following code in Notepad, and save as  .

Copy the codeThe code is as follows:

<?xml version="1.0" encoding="gb2312"?> 
<!----> 
<UserDataList> 
<manager Post="Manager">
        <UserData username="MChooseFlash01" password="MChooseHappiness" /> 
        <UserData username="MChooseFlash02" password="MChooseHappiness" /> 
    </manager> 
<Employee Post="Securities">
        <UserData username="EChooseFlash01" password="EChooseHappiness" /> 
        <UserData username="EChooseFlash02" password="EChooseHappiness" /> 
    </Employee> 
</UserDataList> 

First, analyze the structure:
<UserDataList> is the top-level node. <manager Post="Manager">is a child node under the top-level node. Post is a property of the manager node. <Employee Post="Security"> is a child node under the top-level node, and Post is the property of the Employee node.
<UserData username="MChooseFlash01" password="MChooseHappiness" /> is the child node under the child node under the top-level node, and username and password are attributes of the UserData node.

This time, nested for is also needed.
Open Flash and create a new Flash document with a size of 300*100, save it to the directory of the xml document just now, and name it.
Create 3 new layers and name Actions, cont, and bg.
The bg layer draws three dotted frames of text box size in the first frame. The cont layer pulls two input text boxes in the first frame, with the instance names being username_txt and password_txt respectively, and pulls a dynamic text box, with the instance name being status_txt. Align these three text boxes with dotted frames, click Window->Public Library->Button, and pull a button out. The instance name is login_btn. As shown in the picture:


The Actions layer enters the following code in the first frame:

Copy the codeThe code is as follows:

//. 
//Use system encoding to prevent garbled code.
 = true; 
//Instantiate an XML object.
var myxml:XML = new XML(); 
//Ignore the spaces in the xml document during analysis.
 = true; 
//Load the document.
(""); 
//Call event.
 = function(success:Boolean) 

//If the loading is successful, success=true; otherwise success=false;
    if (success) { 
trace("Loading successfully!");
        login_btn.onRelease = function() 
        { 
//Use an array to refer to the array of child nodes in the top-level node.
            var child_arr:Array = ; 
//Use UserData to specify the attributes object of the xml document node.
            var UserData:Object; 
//This for traverses the child nodes under the top-level node.
            for (var i = 0; i<child_arr.length; i++) { 
//This for traverses the child nodes under the child nodes under the top-level node.
                for (var j = 0; j<child_arr[i].; j++) { 
//Use UserData to specify the child_arr[i].childNodes[j].attributes object
                    UserData = child_arr[i].childNodes[j].attributes; 
//Judge username and password.
//The one here is actually the abbreviation of child_arr[i].childNodes[j]. Similarly.
//username and password are properties in the xml document node. Because xml and Flash are case sensitive, pay attention to case when entering.
                    if ((username_txt.text == ) && (password_txt.text == )) { 
//Post is the Post property of the child node at the top-level node.
status_txt.text= child_arr[i].+":"++"Login successfully";
// If the username and password are correct, exit for. This is important. If you don’t use this, you will continue to judge the username and password repeatedly until the XML document node is traversed. That is not what we want. So exit for as soon as there is a correct one;
                        return; 
                    } else { 
status_txt.text = "Incorrect username or password";
                    } 
                } 
            } 
        }; 
    } else { 
trace("Loading failed!");
    } 
};