Understanding of XML in interaction with FLASH

Source: Internet
Author: User

I. Pay attention to the following points when writing standard XML:
1. A standard XML document must start with "<? Xml> "and a" <? /> "At the end, the first part must declare the XML version, for example, version =" 1.0 ". It should also contain a statement about encoding:" encoding = "UTF-8 "";
2. XML requires that all tags be closed before the end of the document. If the tag contains content, the tag must be in the format of <Tag Name> content </Tag Name>, if the content is blank, it should be written in the format of </Tag Name>; that is, it must be followed by the format of </XXX>.
3. Each XML file has a root element, also known as a root node.
4. nodes have node values and attributes.
5. If the XML node value contains the same characters as the XML tag keyword, XML cannot parse the document. For example, if a node value is greater than or less than a number, the interpreter will generate an error,

2. Create a simple instance in FLASH:

// Create a New XML class instance
Var my_XML: XML = new XML ();
// Create a TextArea component to display the XML content
Var my_TextArea: mx. controls. TextArea;
// Load the external XML file
My_XML.load ("my_xml.xml ");
// Define the onLoad function for the load function --- this is required. XML can be controlled only after loading is successful. Otherwise, no.
My_XML.onLoad = function (OK: Boolean ){
If (OK ){
My_TextArea.text + = this;
} Else {
My_TextArea.text + = "loading failed ..."
}
}
// Ignore the blank space:
My_XML.ignoreWhite = true;

Iii. Explanation of FLASH parsing XML. To put it bluntly, it means reading nodes.
First, let's take a look at this picture and get a little bit of an impression.

Next, I will introduce in detail the parsing of XML files.
For ease of explanation, here is a simple example.
1. Create a New notepad file and enter the following content in it:

<Purchased item>
<Shirt color = "white" brand = "Youngor"> 1 </shirt>
<Pants color = "" brand = "Prince pants"> 1 </pants>
<Shoes color = "black" brand = "Red dragonfly"> 3 </Shoes>
<Total cost>
<Charge> 50 RMB </charge>
<Goods gold> 1000 RMB </goods gold>
</Total cost>
</Purchase items>

Save it as "goods. xml". Note that you must select "Unicode" for encoding ".

2. Next, we will explain the values.
How to read the entire XML file:
Create a FLA file in the same directory of the XML file, and place a TextArea component in the scene. And assign the Instance name "my_TextArea ",
Then add the following code to the first frame:

Var my_XML: XML = new XML ();
System. useCodepage = true;
Var my_TextArea: mx. controls. TextArea;
My_XML.load ("goods. xml ")
My_XML.onLoad = function (OK: Boolean ){
If (OK ){
My_TextArea.text + = this;
} Else {
My_TextArea.text + = "loading failed"
}
}

Press CTRL + ENTER to test the effect.

First, let's take a look at how to read the node name or node attribute in XML?
For example, how do we read the characters "purchase items" in the example?
We use the following statement:
This. firstChild. nodeName;
To read "shirts ":
This. firstChild. childNodes [0]. nodeName;
If you want to read "1 ":
This. firstChild. childNodes [1]. childNodes [0]. nodeValue;
If you want to read the brand of trousers:
This. firstChild. childNodes [1]. attributes. Brand;

Read XML into the LIST component:

System. useCodepage = true;
Var my_TextArea: mx. controls. TextArea;
Var my_List: mx. controls. List;
Var my_XML: XML = new XML ();
My_XML.ignoreWhite = true;
My_XML.load ("shopping. xml ");
My_XML.onLoad = function (OK: Boolean ){
If (OK ){
Var childnodes = this. firstChild. childNodes;
For (I = 0; I <childnodes. length; I ++ ){
My_List.addItem ({label: childnodes. nodeName });
}
} Else {
My_TextArea.text + = "loading failed ";
}
};

First node: firstChild:
Usage: this. firstChild;
Last node: lastChild
Usage: this. lastChild;
Sibling node: nextSibling:
Usage: this. firstChild. childNodes [0]. nextSibling;
Another sibling node: previussibling:
Usage: this. firstChild. childNodes [0]. previussibling;
Difference: nextSibling is backward, while previussibling is before current;
Parent node: parentNode:
Usage: this. firstChild. parentNode;

Demo of all the above instances:
Http://www.taoshaw.com/taoshaw/study/LoadXml/LoadXML.swf

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.