ActionScript 3.0 Series Tutorial (4): Straightforward use of XML

Source: Internet
Author: User
Tags array comments copy execution functions insert object object
xml| Tutorial

ActionScript 3.0 Series Tutorials (3): What does the Document class feature bring to us?

ActionScript 3.0 Series Tutorial (4): Straightforward use of XML

Why give up AS2.0 choose AS3.0? If only I could say three reasons. So AS3.0 's near-perfect support for XML is definitely one of them.

To put it simply, there is a difference between XML support in AS3.0:

. AS2.0 support for XML reluctantly, it will work. and the support of XML in AS3.0 is omni-directional, extremely powerful and flexible.

AS2.0 support for XML is not built-in (build-in), nor is it based on the ECMAScript for XML (E4X) standard. While AS3.0 support for XML conforms to the E4X standard, its design has three advantages:
1. Easy. Includes operation and readability. You will find that the operation of XML in AS3.0 is as straightforward as an ordinary object object. The statement is very plain and smooth.
2. Continuity. The design of its various functions is consistent with the rest of the AS3.0 and is easy to understand.
3. Familiar with. Operators and operational logic are quite familiar to us.
In the AS2.0 era, in order to solve this part of the problem

Efficiency.
Efficiency includes two aspects, development efficiency, and code execution efficiency. Discussion on the efficiency of development. AS3.0 is far more efficient for XML execution than AS2.0 without built-in XML support.

Input of XML

In the AS2.0 era, entering XML code in lines of code is a pain. If it's not read from a file, we're going to put up with a long string of strings that squeeze together.

And in the AS3.0, it's too easy. Directly in accordance with the content of XML to lose, want to change lines on line, want to tab on the tab, just a word, cool.

Create a new FLA, select the first frame, F9 Open the Action panel and enter the following code:

http://www.kingda.org
Example 1
var kingdaxml:xml =
<tutorial>
<item id= ' 1 ' >
<level>2</level>
<title> a Flash 9</title>
</item>
<item id= ' 2 ' >
<level>3</level>
<title> Binding classes</title>
</item>
<item id= ' 3 ' >
<level>4</level>
<title>document class</title>
</item>
</tutorial>
Trace (Kingdaxml.item[1].level); Output:3
Example 2
var ks:string = "<root><txt>this is a test</txt></root>";
var kxml:xml = new XML (KS);
Trace (KXML.txt); Output:this is a test;

Example 1 Note that no, directly write XML content in the back, want to change lines on the line, want to tab on the tab, how cool. When you don't want to write a string in AS2.0, you can't change a line.

By the end of this sentence, we have written a string-like form that is immediately understood by Flash as an XML object, so we can immediately use the word "." operator to access the corresponding property. In this case, the level value of the 2nd item node is accessed.

Is it better to have such a simple and intuitive way of visiting than the AS2.0 childnodes?

But be aware that you can finally add ";" End. But I didn't add to the visual beauty of XML. This does not matter, compile-time will not consider this point.

In fact, as long as you like, AS1.0, 2.0, 3.0 in the end of the statement can not add ";" Resolution But this is not a good programming habit, more inconsistent with the strict requirements of the self grammar. So I suggest that, in addition to XML can not add, the rest should be added, hehe.

Example 2 shows how to convert a string that contains XML content into an XML object. is converted using the constructor of the XML.

AS3 more interesting is that you can use existing variables to directly construct XML, resulting in convenient programming features. The following example.

var rootnodename:string = "Site";
var subnodename:string = "Orgin";
var subnodecontent:string = "Kingda ' s Blog";
var attributename:string = "url"
var attributevalue:string = "http://www.kingda.org";
var extxml:xml =
<{rootnodename} {attributename}={attributevalue}>
<{subNodeName}>{subNodeContent}</{subNodeName}>
</{rootNodeName}>;
Trace (extxml.tostring ());
/*output:
<site url= "http://www.kingda.org" >
<orgin>kingda ' s blog</orgin>
</site>
*/

The point is to enclose the variable in "{}" and not to enclose the attribute in quotes.

External reading of XML

This includes reading an external XML file and reading XML through a URL. The AS3.0 does not integrate a load () like 2.0.
AS3.0 has designed all the external dealings with the URLRequest object, and the data is accepted by the Urlloader object. This we will explain in detail in the next section of the tutorial. This time, as long as you know that the architecture design is thoughtful, and simple and graceful.

var myxml:xml = new XML ();
Initializes an XML address that can be either local "Xxx.xml" or a URL address as follows.
var xml_url:string = "Http://www.kingda.org/blog/index.xml"; My blog RSS feed
var myxmlurl:urlrequest = new URLRequest (Xml_url);
var myloader:urlloader = new Urlloader (Myxmlurl);
Add Mount Completion Listener,
The Event.complete value is "complete" and can be used directly with this string.
Myloader.addeventlistener (Event.complete, xmlloaded);
function xmlloaded (evtobj:event) {
myXML = XML (Myloader.data);
Trace ("Data load completes.");
Trace (myXML);
}

Operation of XML

1. Query

Display the title value of a node with a level of 4
Trace (Kingdaxml.item. ( Level = = 4). title);
Output:document Class
Displays the title value of the Level>2 node, where the result is greater than 1, so it is an XML Array.
Trace (Kingdaxml.item. ( Level > 2). title);
/*output:
<title>binding classes</title>
<title>document class</title>
*/

Use a property to begin with a @. That's convenient.
Trace (Kingdaxml.item. ( Level > 2). @id);
Output:23
Notice here, it's actually 2, 3. An array.
You can also use attributes to make judgments.
Trace (Kingdaxml.item. ( @id > 1). title);

2. Add or modify attributes
Convenient can not be no longer convenient, direct write can. It's so cool.

Change the node level value of id = = 1 to 2
Kingdaxml.item. (@id ==1). level = 2;
Add an attribute to the Id==1 node page
Kingdaxml.item. (@id ==1). page = 100;
Trace (Kingdaxml.item. ( @id ==1));

3. Insert a node by a condition

var newnode1:xml = <item id= ' 2.5 ' ><level>0</level><title>None</title></item>
var newnode2:xml = <item id= ' 1.5 ' ><level>0</level><title>None</title></item>
Insert the newNode1 behind the id==2 node
Kingdaxml = Kingdaxml.insertchildafter (Kingdaxml.item. ( @id ==2), newNode1);
Insert the newNode1 in front of the id==2 node
Kingdaxml = Kingdaxml.insertchildbefore (Kingdaxml.item. ( @id ==2), newNode2);
Trace (Kingdaxml);

Advanced Operations FOR XML

The most commonly used operations are clearly described above. The advanced operations are left to the brothers who have a deeper application to XML.

A few notes:
1. In AS3.0, the ignorewhitespace of the XML class defaults to True.
2.AS3.0 supports direct manipulation of comments. But the default:

Xml.ignorecomments = false;
var kingdaxml:xml =
<item>
<!--comment 1-->
<!--comment 2-->
</item>;
Trace (kingdaxml.toxmlstring ()); When the default is true, the comment is not displayed

Access Comment with

Trace (Kingdaxml.comments () [1].toxmlstring ());

3.XML supports cloning.

Use Copy () to get a copy of the value of an existing XML.

var kingdacopy:xml = kingdaxml.copy ();

The kingdacopy operation will not affect the Kingdaxml object.

4. The most useful descendants function returns a XMLList object, including all child nodes.
Set ignorecomments = false; and ignoreprocessinginstructions = False, even comments and process instructions are included in this XMLList object.

Use the example below:

Xml.ignorecomments = false;
var xml:xml =
<body>
<!--comment-->
Text1
<a>
<b>text2</b>
</a>
</body>;
Trace (xml.descendants ("*"). Length ()); 5
Trace (xml.descendants ("*") [0]); <!--comment-->
Trace (xml.descendants ("*") [1].toxmlstring ()); Text1
Trace (Xml.descendants ("a"). toxmlstring ()); <a><b>text2</b></a>
Trace (xml.descendants ("B"). Toxmlstring ()); <b>text2</b>

There are too many XML useful operational functions (such as operations on namespace). When you use it, go over the reference books.
The above introduction can satisfy most of the application.

For the AS2.0 existing XML class, in 3.0 into the XmlDocument class, the use of the same method. Easy to AS2.0 program porting. The rest is not recommended.

Next time, talk about the 3.0 web interaction model and usage.



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.