Tips for using custom Namespaces-namespaces and XML

Source: Internet
Author: User
Tags hasownproperty

In some as3 Projects
XML is a frequently used method to store data.
We often use XML to save some data or set some parameters. Friends who use Flex are even more familiar with the enhanced customization of mxml. Then write the code
We have to write some code to parse XML content frequently. Today I want to talk about a small trick. It is also a namespace just sorted out to facilitate XML parsing. A friend has learned from lex's mxml that this method can be used.



I. Conversion of variable values of complex types




In XML, we all know that after the XML class of as3 is read in, all its values areString

. Therefore, you want to store arrays or
Composite attributes in the UI
We need
Process these strings to convert them to other variable types of as3.
. For example, the following XML:

<Object>
<ID> 1000 </ID>
<Name> test </Name>
<Childidlist> 235,235,464, </childidlist>
</Object>


Internally, if we want to convert XML data to specific attributes in the myclass class, we may write the following functions:
The first is the myclass class:

Package {
Public class myclass {
Public var ID: int;
Public var name: string;
Public var childidlist: array = new array;
Public Function myclass (){
}
}
}

Method 1:

Public Function buildobjectbyxml (data: XML): void {
VaR myobject: myclass = new myclass;
Myobject. ID = data. ID as int;
Myobject. Name = data. Name as string;
Myobject. childidlist = data. childidlist. Split (",");
}


Method 2:

Function buildobjectbyxml (data: XML): void {
VaR myobject: myclass = new myclass;
VaR nodelist: xmllist = data. Children ();
For each (VAR node: XML in nodelist ){
VaR valuename: String = node. localname ();
If (! Myobject. hasownproperty (valuename) continue;
If (valuename = "childidlist "){
Myobject [valuename] = node. tostring (). Split (",");
} Else {
Myobject [valuename] = node. tostring ();
}
}
}

The first method is to convert all attributes of human flesh and then treat them differently. The second method is to traverse the elements in XML and use the if condition to filter and then perform the specific difference and conversion. All conversion processes and Algorithms
All are concentrated in the buildobjectbyxml function. Its disadvantage is that
In case of a large number of complex attributes in the XML to be set, the readability will go down. For example, we are writing the UI component of the XML configuration.
This is often the case. In this case, it would be better if the variable is fixed and less modified later. If frequent modification is required, we have to face a very long and complex and difficult-to-read buildobjectbyxml function.


So how can I use a namespace to optimize this structure? First,
XML itself also has namespaces

(For more information, see as3 help ). When the second namespace is differentFunctions or variables with the same name can be used.

. We can transform the above XML and myclass into the following forms:
First, XML:

<Object xmlns: xmlset = "http://www.aslii.com/xmlset”>
<ID> 1000 </ID>
<Name> test </Name>
<Xmlset: childidlist> 235,235,464, </xmlset: childidlist>
</Object>


Next is the myclass class:

Package {
Public class myclass {
Public namespace xmlset = http://www.aslii.com/xmlset;
Public var ID: int;
Public var name: string;
Public var childidlist: array = new array;
Public Function myclass (){
}
Xmlset function set childidlist (datastring: string): void {
This. childidlist = datastring. Split (",");
}
}
}


Finally, our buildobjectbyxml function:

Function buildobjectbyxml (data: XML): void {
VaR myobject: myclass = new myclass;
VaR nodelist: xmllist = data. Children ();
For each (VAR node: XML in nodelist ){
VaR valuename: String = node. localname ();
VaR ns: namespace = node. namespace ();
If (myobject. hasownproperty (valuename )){
Myobject. NS: [valuename] = node. tostring ();
}
}
}


We can see that the code of the specific conversion type is moved to a fixed namespace in a specific class and the same name as the attribute exists. In addition, if you add or delete attributes later, you only need to add the corresponding methods and variables to a specific class,
The buildobjectbyxml function does not need to be modified.
. If the algorithm changes during conversion, we can easily find the set function with the corresponding function name in the object class.


2. Use namespace + XML to build your own component configuration file
.




Friends who write the UI should be interested in this part. In fact, the content is basically the method of copying mxml. Careful friends may have discovered that the structure of flex mxml is very frequent.
But its mxml can only be used in the compilation phase and finally compiled into SWF. After being compiled into SWF, we can no longer use mxml. It uses XML
The process of converting a namespace to an instance is solidified in the flex editor rather than the SWF dynamic compilation. Therefore, we cannot see how the conversion code is written.
In fact, the principle is the same as that of the Set function that uses the namespace to specify a specific namespace, we only need to associate the algorithm in the namespace specified function with the creation component when parsing XML.
The specific writing method is actually to write a set of top-level namespace functions.
For example, to write a set of LII components, You need to configure the interface through XML. The following is a simple implementation:
First, XML:

<Root xmlns: LII = "http://www.aslii.com/liiUI">
<LII: button id = "XX" x = "0" Y = "0" width = "232" Height = "333"/>
</Root>


Then there is a top-level class of the default package:

Package {
Public class liiuimanager {
Public static const core: liiuimanager = new liiuimanager ();
Public namespace LII = "http://www.aslii.com/liiUI ";
Public var uilist: array = new array;
Public var length: int;
Public Function liiuimanager (){
}
Lii static function set button (data: XML): void {
Trace ("[New LII: button]");
Core. uilist [length] = new mybutton ();
Core. Length + = 1;
// The following uses XML to parse and convert the property code and construct the code in detail.
}
Public static function buildui (data: XML): void {
VaR nodelist: xmllist = data. Children ();
For each (VAR node: XML in nodelist ){
VaR valuename: String = node. localname ();
VaR ns: namespace = node. namespace ();
Liiuimanager. NS: [valuename] = node;
}
}
}
}


Finally, execute the parsing:

Liiuimanager. buildui (XML );


 
In addition, we can write them as real-time parsing and construction that can be dynamically compiled, instead of being compiled and constructed by the editor as flex does. This is just a small example. You can make full use of your imagination and creativity. Flex mxml is not mysterious. It is so simple that we can also have it.

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.