Flash process XML Document Data tutorial

Source: Internet
Author: User
Tags array definition exit processing instruction requires
xml| Tutorials | data

Prepare: First you have to have a computer, next is to install the Windows system and FLASH8.

XML Basics:

Now that you're working with an XML document, you need to understand the XML document first. Here's what I've learned, some of which are not available in Flash.

XML Extensible Markup Language (extensible Markup Language) is a subset of SGML (structured generalized Markup Language), by World Wide Web Consortium XML Workgroup definition.

Let's begin to understand the structure of XML. XML is made up of preamble and document elements.

The preamble includes:
1. Declaration (Declaration): The first line of a document that states that it is an XML document. [Optional]
2. Notes: Enhances the readability of the document. [Optional]
3. Document type declaration: The XML document type definition must precede the XML declaration, before the document element, and the middle can insert an XML processing instruction that describes the document's type and structure. [Optional]
4. Processing instruction (processing instruction): An instruction that processes XML information after an XML declaration. [Optional]
Note: The document type declaration (declaration) requires an XML glossary. Please visit http://www.w3c.orgIf you want to know more details.

Document elements:
The elements describe the logical structure of the document, the start tag of the element package, the element content, the element attributes, and the end tag. An XML document must have a top-level element, and all elements are nested within this top-level element. The document element name, the property name, are all custom.

Description
First look at the code: <?xml version= "1.0" encoding= "gb2312" standalone= "yes"?>
<!--XML annotation-->
<! DOCTYPE chooseflash>
<!--<! DOCTYPE type System "address of a DTD file" > No address or address error can be faulted, System one keyword-->
<?xml-stylesheet type= "Text/css" href= "CSS.CSS to process XML Document Information"?>
< elements >
< child element properties = "Child elements" > Information </child elements >
</elements >

Above is a simple XML document with a document type declaration and processing instruction. Below one by one explains.

<?xml version= "1.0" encoding= "gb2312"?>

This is the declaration information for XML. Version is the edition number, encoding is the character encoding, if there is a Chinese need to use gb2312 encoding, standalone properties can take yes and no, the property value Yes, the document does not have an external declaration. property value No, which indicates that the document has an external declaration.

<!--XML annotation-->

This is a comment in XML.

<! DOCTYPE chooseflash>

This is a document type declaration, where the DOCTYPE tag is used for declaring a type, and Chooseflash is a type, where the type refers to the glossary in XML.

<?xml-stylesheet type= "Text/css" href= "CSS.CSS to process XML Document Information"?>

This sentence is a processing instruction, Xml-stylesheet is a processing instruction, type is a kind, and href is an address. This processing instruction links CSS to XML documents, similar to the CSS applied in HTML.

< elements >
< child element properties = "Child elements" > Information </child elements >
</elements >

This is the document element (Flash is represented by a node). < element > is a top-level element,< > is an element that is nested in a top-level element, and "attribute" is a property, "Information" is a string, or it can be written like this:
< child element attribute = "child element" information = "info"/>
If a document type declaration is used, the top-level element naming is best followed by the type of declaration. Otherwise, there will be an error (when there is a glossary, like this Chooseflash this will not go wrong because there is no glossary.) )

The following is a complete XML document: <?xml version= "1.0" encoding= "gb2312"?>
<!--Firstnode is a top-level element-->
<firstNode>
<!--Childnode is a child element nested in the firstnode of the top-level element, with a nodename attribute-->
<childnode nodename= "Childnode" >childNode</childNode>
</firstNode>

The XML aspect is here, and here's how to invoke the XML document and process the data in the XML in Flash.

Flash aspect::
Calling an XML document in Flash requires some of the methods and properties in the XML class and the XmlNode class. We don't have to use that much here. List the methods and attributes that are used:

XML class:
xml.ignorewhite;//handles whitespace in an XML document, when set to true, ignores whitespace and defaults to false.
Xml.load ("Address of XML document");//load the specified XML document.
Xml.onload=function (Success:boolean) {};//Called when the XML document was successfully loaded.
XML constructor.

XmlNode class:
xmlnode.attributes;//is used to specify the attributes of an XML document.
xmlnode.childnodes;//returns an array of children of the specified XML Document object.
xmlnode.firstchild;//refers to the first child in the child list of the parent node.
xmlnode.nodevalue;//returns the node value of an XML object.
Xmlnode.nodename;//xml object's node name

Start Experiment:
First Note: Add encoding= "gb2312" at the XML declaration, use GB2312 encoding, if there is Chinese in flash, you need to add System.usecodepage = True before loading code, use system code to prevent garbled.

Experiment 1:
A simple experiment, the node in the XML document in the flash output. Create a new XML document and enter the following code in Notepad. Save As Xml-001.xml.

<?xml version= "1.0"?>
<!--xml-001.xml-->
<firstnode name= "1" >
<childnode name= "1.1"/>
<childnode name= "1.2"/>
<childnode name= "1.3"/>
</firstNode>

Above is a simple XML document, which is a nested three child nodes in a top-level node.

How do you read it in Flash now? To see the operation: Open Flash, create a new flash document, saved to the directory in the XML document just now, named Xml-001.fla. Enter the following code in the first frame:

Xml-001.fla.
Instantiates an XML object.
var myxml:xml = new XML ();
Whitespace in an XML document is ignored during profiling.
Myxml.ignorewhite = true;
Loads the Xml-001.xml document.
Myxml.load ("Xml-001.xml");
Invokes the Xml.onload event.
Myxml.onload = function (Success:boolean)
{
If the load succeeds, success=true; otherwise success=false;
if (success) {
Trace ("Load succeeded!");
Outputs the node name of the top-level node and the value of the attribute name in the top-level node.
Trace (myxml.firstchild.nodename+ ":" +myxml.firstchild.attributes.name);
Use an array to refer to an array of neutron-level nodes of the top-level node.
var child_arr:array = myxml.firstChild.childNodes;
Iterates through all the data in an XML document with a nested for statement.
This for traversal is a child node under the top-level node.
for (var i = 0; i<child_arr.length; i++) {
The node name of the child node under the top node of the output and the value of the attribute name in the child node under the top-level node.
Trace (child_arr[i].nodename+ ":" +child_arr[i].attributes.name);
}
} else {
Trace ("Load failed!");
}
};

Experiment 2:
What we're doing now is to lose the XML document of a multi-tiered nested node in Flash. Create a new XML document, enter the following code in Notepad, and save it as Xml-002.xml.

<?xml version= "1.0"?>
<!--xml-002.xml-->
<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>

Above is a multi-layer nested node XML document, the structure is a top-level node, nested 3 child nodes, 3 child nodes nested 3 child nodes respectively. Now how should you read it in Flash? In fact, the principle is the same as above, nested a for in the for is OK.

Open Flash Create a new flash document and save it to the directory in the XML document that you just named Xml-02.fla. Enter the following code in the first frame:

Xml-002.fla.
Instantiates an XML object.
var myxml:xml = new XML ();
Whitespace in an XML document is ignored during profiling.
Myxml.ignorewhite = true;
Loads the Xml-002.xml document.
Myxml.load ("Xml-002.xml");
Invokes the Xml.onload event.
Myxml.onload = function (Success:boolean)
{
If the load succeeds, success=true; otherwise success=false;
if (success) {
Trace ("Load succeeded!");
Outputs the node name of the top-level node and the value of the attribute name in the top-level node.
Trace (myxml.firstchild.nodename+ ":" +myxml.firstchild.attributes.name);
Use an array to refer to an array of neutron-level nodes of the top-level node.
var child_arr:array = myxml.firstChild.childNodes;
Iterates through all the data in an XML document with a nested for statement.
This for traversal is a child node under the top-level node.
for (var i = 0; i<child_arr.length; i++) {
The node name of the child node under the top node of the output and the value of the attribute name in the child node under the top-level node.
Trace (child_arr[i].nodename+ ":" +child_arr[i].attributes.name);
This for traversal is a child node under a child node under the top-level node.
for (var j = 0; j<child_arr[i].childnodes.length; j + +) {
The node name of the child node under the child node under the top node of the output and the value of the attribute name in the child node under the child node under the top-level node. Don't faint. Look at the output panel and you'll understand the relationship.
Trace (child_arr[i].childnodes[j].nodename+ ":" +child_arr[i].childnodes[j].attributes.name);
}
}
} else {
Trace ("Load failed!");
}
};

Experiment 3:
Use XML document data to do user login.
Create a new XML document, enter the following code in Notepad, and save it as Xml-003.xml.

<?xml version= "1.0" encoding= "gb2312"
<!--xml-003.xml-->
<userdatalist>
     <manager post= "Manager"
        <userdata Username= "MChooseFlash01" password= "mchoosehappiness"/>
         <userdata username= "MChooseFlash02" password= "mchoosehappiness"/>
    </manager
    <employee post= "Staff"
         <userdata username= "EChooseFlash01" password= "echoosehappiness"/>
         <userdata username= "EChooseFlash02" password= "echoosehappiness"/>
     </employee>
</userdatalist>

First, analyze the following structure:
<UserDataList> is the top level node. <manager post= "Manager" > is a child node under the top-level node. Post is the property of the manager node. <employee post= "Staff" > is a child node under the top-level node, and Post is the attribute of the employee node.
<userdata username= "MChooseFlash01" password= "mchoosehappiness" is a child node under a child node under the top-level node, Username and password are attributes of the UserData node.

Nested for is also needed this time.
Open Flash Create a new flash document, size 300*100, saved to the directory of the XML document just now, named Xml-03.fla.
Create a new 3 layer named Actions, Cont, BG, respectively.
The BG layer draws a dotted box of three text box sizes in the first frame. Cont layer in the first frame pull two input text boxes, instance names are Username_txt and password_txt, pull a dynamic text box, instance named Status_txt. Align the three text boxes to the dotted box, then click the-> Common Library-> button and pull a button out. Instance name is LOGIN_BTN. As shown in figure:

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

Xml-003.fla.
Use system encoding. Prevent garbled characters.
System.usecodepage = true;
Instantiates an XML object.
var myxml:xml = new XML ();
Whitespace in an XML document is ignored during profiling.
Myxml.ignorewhite = true;
Loads the Xml-002.xml document.
Myxml.load ("Xml-003.xml");
Invokes the Xml.onload event.
Myxml.onload = function (Success:boolean)
{
If the load succeeds, success=true; otherwise success=false;
if (success) {
Trace ("Load succeeded!");
Login_btn.onrelease = function ()
{
Use an array to refer to an array of neutron-level nodes of the top-level node.
var child_arr:array = myxml.firstChild.childNodes;
Specifies the attributes object for the XML document node with UserData.
var userdata:object;
This for traversal is a child node under the top-level node.
for (var i = 0; i<child_arr.length; i++) {
This for traversal is a child node under a child node under the top-level node.
for (var j = 0; j<child_arr[i].childnodes.length; j + +) {
Specify Child_arr[i].childnodes[j].attributes object with UserData
UserData = child_arr[i].childnodes[j].attributes;
The user name and password are judged.
The userdata.username here is actually a shorthand for child_arr[i].childnodes[j].attributes.username. Userdata.password.
Username and password are attributes in an XML document node. Because XML and flash are case-sensitive. So be careful about the case when you type.
if ((Username_txt.text = = userdata.username) && (Password_txt.text = = Userdata.password)) {
Post this is the Post property of the node at the top level of the node.
Status_txt.text = Child_arr[i].attributes. post+ ":" +userdata.username+ "login Success";
If the username and password are correct, exit the for. This is important. If you do not use this, you will always repeat the user name and password. Until the XML document node is traversed. That's not what we want. So the right exit for;
Return
} else {
Status_txt.text = "User name or password error";
}
}
}
};
} else {
Trace ("Load failed!");
}
};

Example 4:
Use the list component to make a MP3 playlist.
Create a new XML document, enter the following code in Notepad, and save it as Xml-004.xml.

<?xml version= "1.0" encoding= "gb2312"
<mp3list>
    <mp3 path= " Jmzcg.mp3 "> Asan-Loneliness in singing </mp3>
    <mp3 path=" Jmjywsls.mp3 "> Loneliness is because of who I miss </mp3
    <mp3 path= "Szwspdxd.mp3" > Old Wolf-sleeping on my upper bunk brother </mp3>
     <mp3 path= "Qf.mp3" > Oath-seeking Buddha </mp3>
    <mp3 path= "Xyzth.mp3" > Next Stop Days </mp3>
    <mp3 path= "Nswdxfm.mp3" > Yi Jing-you are my happiness </mp3>
     <mp3 path= "Aqfx.mp3" > Love Revival (Joey) </mp3>
    <mp3 path= "Qrlg.mp3" > Wood Love-Autumn Sonata </mp3>
    <mp3 path= "Qnyh.mp3" > Leslie Cheung-Qian female ghosts </mp3>
     <mp3 path= "A.mp3" > A great English song (often put in bars) </mp3>
</mp3list>

XML Structure:
<mp3list>xml the top-level node in the document.
<mp3 path= "Jmzcg.mp3" > Asan-Loneliness in singing </mp3> child nodes under the top-level node, path is the attribute under the MP3 node, "Asan-loneliness in singing" is the MP3 node value.

Open Flash Create a new flash document, size 200*150, saved to the directory of the XML document just now, named Xml-04.fla. New 2 layers Name the actions, Cont, and the Cont layer to pull a list component to the first frame. The size is 200*150, the instance name is the first frame of the mp3_list,actions layer, enter the following code:

Xml-004.fla.
Use system encoding. Prevent garbled characters.
System.usecodepage = true;
Instantiates an XML object.
var myxml:xml = new XML ();
Whitespace in an XML document is ignored during profiling.
Myxml.ignorewhite = true;
Loads the Xml-004.xml document.
Myxml.load ("Xml-004.xml");
Invokes the Xml.onload event.
Myxml.onload = function (Success:boolean)
{
If the load succeeds, success=true; otherwise success=false;
if (success) {
Trace ("Load succeeded!");
Use an array to refer to an array of neutron-level nodes of the top-level node.
var child_arr:array = myxml.firstChild.childNodes;
This for traversal is a child node under the top-level node.
for (var i = 0; i<child_arr.length; i++) {
Add the song to the list component.
Child_arr[i].firstchild.nodevalue This is the node value of the XML node.
Child_arr[i].attributes.path This is the path attribute under the XML node.
Mp3_list.additem ({label: (i+1) + "." +child_arr[i].firstchild.nodevalue, Data:child_arr[i].attributes.path});
}
} else {
Trace ("Load failed!");
}
};
The Listevent function is to perform the Change event in the list component
function Listevent (): Void
{
This is the song address. Use the Loadsound method of sound class to play the song. Example: Mysound.loadsound (mp3_list.selectedItem.data); see the help documentation for details.
Trace (Mp3_list.selectedItem.data);
}
Add a frame listener.
Mp3_list.addeventlistener ("Change", listevent);



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.