SoFunction
Updated on 2025-04-13

A relatively complete and concise tutorial on processing XML document data in Flash Previous article Page 2/3


Flash::
Calling an XML document in Flash requires some methods and properties in the XML class and XMLNode class. We don't need to use that much here. List the methods and properties used:

XML class:
//When processing blanks in xml documents, when set to true, the blanks are ignored, and the default value is false.
("The address of the xml document");//Load the specified xml document.
=function(success:Boolean){};//Called when the xml document is successfully loaded.
XML constructor.

XMLNode class:
;//Use to specify the properties of the xml document.
;//Returns an array of children of the specified xml document object.
;//Refers to the first child in the parent node's child list.
;//Returns the node value of the XML object.
;//The node name of the XML object

Start the experiment:
First of all, add encoding="gb2312" to the xml declaration and use gb2312 encoding; if there is Chinese in Flash, you need to add = true before the loading code; use system encoding to prevent garbled code.

Experiment 1:
A simple experiment, output nodes in the XML document in Flash. Create a new XML document and enter the following code in Notepad. Save as   .

Copy the codeThe code is as follows:

<?xml version="1.0"?> 
<!----> 
<firstNode name="1"> 
    <childNode name="1.1" /> 
    <childNode name="1.2" /> 
    <childNode name="1.3" /> 
</firstNode> 

The above is a simple XML document, the structure is to nest three child nodes in a top-level node.

How to read it in Flash now? Let’s take a look at the operation: Open Flash, create a new Flash document, save it to the directory in 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].); 
        } 
    } else { 
trace("Loading failed!");
    } 
}; 

Experiment 2:
What we are doing now is to output the XML document of a multi-layer nested node in Flash. 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"?> 
<!----> 
<firstNode name="1"> 
    <childNode name="1.1"> 
        <Node name="1.1.1" /> 
        <Node name="1.1.2" /> 
        <Node name="1.1.3" /> 
    </childNode> 
    <childNode name="1.2"> 
        <Node name="1.2.1" /> 
        <Node name="1.2.2" /> 
        <Node name="1.2.3" /> 
    </childNode> 
    <childNode name="1.3"> 
        <Node name="1.3.1" /> 
        <Node name="1.3.2" /> 
        <Node name="1.3.3" /> 
    </childNode> 
</firstNode> 
The above is an XML document with a multi-layer nested node. The structure is that in a top-level node, there are 3 child nodes nested, and 3 child nodes nested with 3 child nodes respectively. How should I read it in Flash now? In fact, the principle is the same as above, just nest a for in the for.
Previous page123Next pageRead the full text