Classes that convert the XML inside of Flash into objects
Source: Internet
Author: User
Xml| Object | Transforms the most recently written class for parsing flash internal XML and converting it into objects. Share with you.
I have used it several times to parse more complex XML objects.
The Xmltoarray class code is as follows, please save as xmltoarray.as:
Import Mx.events.EventDispatcher;
Class Xmltoarray
{
Private Var data:object, Xml:object;
public Var tem_nodes:xml;
function Xmltoarray (tn:xml)
{
Tem_nodes = TN;
Tem_nodes.ignorewhite = true;
}
function Toobject (): Object
{
var data:object = parse (tem_nodes);
return data;
}
Private Function Parse (node:xmlnode): Object
{
var node:xmlnode = tem_nodes;
var value:object = new Object ();
if (node.childNodes.length!= (undefined | | null))
{
var nodes:number = node.childNodes.length;
var type:string = "xml"
} else
{
var temp_array = node;
var nodes:number = temp_array.length;
var type:string = "Array"
}
for (var i = 0; I!= nodes + i)
{
if (type = = "xml"
{
var name:string = node.childnodes [I].nodename;
} else
{
var name:string = node [i].nodename;
}
if (name!= null)
{
if (value [name]!= undefined)
{
if (! (value [name] instanceof Array)
{
value [Name] = new Array (value [name]);
}
if (type = = "xml"
{
Value [Name].push (GetValue (node.childnodes [i]));
} else
{
Value [Name].push (GetValue (node [i]));
}
} else
{
if (type = = "xml"
{
value [Name] = GetValue (node.childnodes [i]);
} else
{
value [Name] = getValue (node [i]);
}
}
} else
{
if (type = = "xml"
{
Value = GetValue (Node.childnodes [i]);
} else
{
Value = getValue (node [i]);
}
}
}
var attributes:object = getattributes (node);
if (attributes!= null)
{
if (nodes!= 0)
{
if (! (Value instanceof XMLNode))
{
for (var i in value)
{
attributes [i] = value [i];
}
} else
{
attributes [' _val '] = value.nodevalue;
}
}
return attributes;
}
return value;
}
Private Function GetAttributes (node:xmlnode): Object
{
var attributes = new Object ();
for (var i in node.attributes)
{
attributes [i] = node.attributes [i];
}
Return i!= undefined? Attributes:null;
}
Private Function GetValue (node:xmlnode): Object
{
Switch (node.nodetype)
{
Case 1:
Return parse (node);
Case 3:
return node.tostring ();
}
return null;
}
Private Function Finalize ()
{
Delete xml;
}
Usage
Import Xmltoarray;
var xml = new XML ("<CourseWare><name>AAAAA</name></CourseWare>");
Xml.ignorewhite = true;
var my_xml = new Xmltoarray (XML);
var a:object = My_xml.toobject ();
A is the converted object.
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.