//////////////////////////////////////// ////////////////////////////////////////
//
// My system
// Copyright 2008-2009 my systems inconfigurated
// All Rights Reserved.
//
// Notice: My system permits you to use, modify, and distribute this file
// Langversion ActionScript 3.0
// Playerversion, flash 9.0
// Date: 2008-12-02 Keren
// QQ: 994251681
// MSN: keren1515@hotmail.com
//
//////////////////////////////////////// ////////////////////////////////////////
// Summary of the x4e attribute access method:
// Node. @ ID
// Node. Attribute ("ID ")
// Node. ['@ id']
// Node. @ ['id']
//////////////////////////////////////// ////////////////////////////////////////
Package org. App. utils. Common
{
Public class myxmlutils
{
Private Static Var instance: myxmlutils = NULL;
Public static function getinstance (): myxmlutils {
If (instance = NULL ){
Instance = new myxmlutils (New singletonenforcer ());
}
Return instance;
}
Public Function myxmlutils (Enforcer: singletonenforcer)
{
If (enforcer = NULL) throw new error ("this class is singletonenforcer .");
}
//////////////////////////////////////// ////////////////////////////////
/**
* Parses a string into an XML object.
*/
Public Function createxml (STR: string,
Ignorecommnets: Boolean = false,
Ignorewhitespace: Boolean = false): XML {
Try {
XML. ignorecomments = ignorecommnets;
XML. ignorewhitespace = ignorewhitespace;
VaR XML: xml = New XML (STR );
Return XML;
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
//////////////////////////////////////// ////////////////////////////////
/**
* Create a New XML Node object
* @ Param string nodename node name
* @ Param string nodevalue node Value
* @ Return New Node object built in XML
*/
Public Function createsinglenode (nodename: String, nodevalue: string): XML {
Try {
VaR STR: String = "<" + nodename + ">" + nodevalue + "</" + nodename + "> ";
Return New XML (STR );
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
/**
* Create a series of node objects
* @ Param string nodename node name
* @ Param array nodevalue node value Array
* @ Return New Node object built by xmllist
*/
Public Function createnodelist (nodename: String, nodevalue: array): xmllist {
Try {
VaR xmllist: xmllist = new xmllist ();
VaR node: XML;
For (var I: Int = 0; I <nodevalue. length; I ++ ){
Node = This. createsinglenode (nodename, nodevalue [I]);
Xmllist + = node;
}
Return xmllist;
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
//////////////////////////////////////// ////////////////////////////////
/**
* Mutual conversion between XML and xmllist
*/
Public Function toxml (xmllist: xmllist): XML {
Return New XML ("<>" + xmllist. toxmlstring () + "</> ");
}
Public Function toxmllist (XML: XML): xmllist {
VaR xmllist: xmllist = new xmllist ();
Xmllist + = xml;
Return xmllist;
}
/**
* Convert an XML object to a string
*/
Public Function toxmlstring (XML: *): String {
Try {
VaR Res: String = "";
If (XML! = NULL & XML is XML ){
Res = xml. toxmlstring ();
} Else if (XML! = NULL & XML is xmllist ){
VaR item: XML;
For each (item in XML ){
Res + = item. toxmlstring ();
}
}
Return res;
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
/**
* Return a text string, for example, <A> AAA </a>. The return value is AAA;
*/
Public Function tostring (XML: *): String {
Try {
VaR Res: String = "";
If (XML! = NULL & XML is XML ){
Res = xml. tostring ();
} Else if (XML! = NULL & XML is xmllist ){
VaR item: XML;
For each (item in XML ){
Res + = item. tostring ();
}
}
Return res;
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
//////////////////////////////////////// ////////////////////////////////
/**
* Add the child node information to the end of the target XML to make the newly added node the final node [only for the first layer of child nodes under the root node]
* @ Param src_xml new node information XML Object
* @ Param target_xml the target XML object to be operated on
* @ Return Boolean true: Success false: Failure
*/
Public Function appendnode (src_xml: *, target_xml: XML): Boolean {
Try {
If (target_xml! = NULL & target_xml is XML ){
If (src_xml is XML | src_xml is xmllist ){
Target_xml.appendchild (src_xml );
Return true;
} Else if (src_xml is string ){
VaR XML: xml = New XML (src_xml );
Target_xml.appendchild (XML );
Return true;
}
}
} Catch (error: Error ){
Throw error;
}
Return false;
}
//////////////////////////////////////// ////////////////////////////////
/**
* Obtain the node name of the first subnode of the target XML object.
* @ Param XML the XML object to be operated on
* @ Return string node name
*/
Public Function getsubnodename (target_xml: XML): String {
Try {
If (target_xml = NULL |! (Target_xml is XML) return NULL;
VaR xmllist: xmllist = target_xml.children ();
If (xmllist = NULL) return NULL;
VaR subnode: xml = NULL;
For each (subnode in xmllist ){
Break;
}
If (subnode! = NULL & subnode is XML ){
Return subnode. Name ();
}
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
/**
* Obtain the name of a node.
*/
Public Function getnodename (node: XML): String {
Return node. Name ();
}
/**
* Get the node Value
*/
Public Function getnodevalue (node: XML): String {
Return node. tostring ();
}
//////////////////////////////////////// ////////////////////////////////
/**
* Insert new node information at the specified position and determine the insertion position based on before;
* 1. If no location is specified, insert the child node information to the first location of the first layer under the first root node of the target XML file,
* Make the newly added node the first node.
*
* [Note] It is for the first layer of subnodes under the root node.
* @ Param string src_xml new node information. If it is a string, it must be a single-node string,
* Eg: src_xml = "<A0> 00000 </A0> ";
* @ Param XML target_xml the target XML object to be operated on
* @ Param int position insert position
* @ Param Boolean before true: after the specified position
* False: before the specified position
*
* @ Return Boolean true: Success false: Failure
*/
Public Function insertnode (src_xml: *, target_xml: XML,
Position: Int = 0, before: Boolean = false): Boolean {
Try {
If (target_xml = NULL |! (Target_xml is XML )){
Return false;
}
VaR O: Object = NULL;
If (src_xml is XML | src_xml is xmllist ){
O = src_xml;
} Else if (src_xml is string ){
O = New XML (src_xml );
}
// Number of lower-level nodes of the current target object
VaR count: Int = target_xml.length ();
Trace ("-- count:" + count );
If (count <= 0 ){
// The new node becomes the first subnode
Target_xml.prependchild (O );
Return true;
}
// Node location processing
If (position <= 0 ){
// The new node becomes the first subnode
Target_xml.prependchild (O );
Return true;
}
// Obtain the subnode name
VaR nodename: String = This. getsubnodename (target_xml );
If (position> = count ){
Position = count-1;
}
VaR tmpxml: xml = This. getchildbynodenameandposition (target_xml, nodename, position );
VaR xmllist: xmllist = This. toxmllist (tmpxml );
Trace ("=" + xmllist. tostring ());
If (Before) {// before
Target_xml.insertchildbefore (xmllist, O );
} After else {//
Target_xml.insertchildafter (xmllist, O );
}
} Catch (error: Error ){
Throw error;
}
Return true;
}
//////////////////////////////////////// ////////////////////////////////
/**
* Get the Node object at the specified position
* [Note] only the first subnode under the root node can be used.
*
* @ Param XML the XML object for the target_xml operation
* @ Param int position
*
* @ Return XML target node information [XML]
*/
Public Function getchildbyposition (target_xml: XML, position: INT): XML {
Try {
VaR xmllist: xmllist = target_xml.children ();
If (xmllist = NULL |! (Xmllist is xmllist) return NULL;
Trace (xmllist. Length ());
If (position <0 | position> xmllist. Length () return NULL;
VaR item: XML;
VaR index: Int = 0;
For each (item in xmllist ){
// Trace ("item:" + item. toxmlstring ());
If (position = index) return item;
Index ++;
}
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
/**
* Obtain a list of node names.
* [Note] only the first subnode under the root node can be used.
*/
Public Function getchildbynodename (target_xml: XML, nodename: string): xmllist {
Return target_xml.child (nodename );
}
/**
* Get the Node object at the specified position
* [Note] only the first subnode under the root node can be used.
*
* @ Param XML the XML object for the target_xml operation
* @ Param string nodestring node name
* @ Param int position
*
* @ Return XML target node information [XML]
*/
Public Function getchildbynodenameandposition (target_xml: XML,
Nodename: String, position: INT): XML {
Try {
Return target_xml.child (nodename) [position];
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
/**
* Get the XML Object of the specified node name and node Value
* [Note] only the first subnode under the root node can be used.
*/
Public Function getchildbynodenameandnodevalue (target_xml: XML,
Nodename: String, nodevalue: string): XML {
Try {
// Determine whether the node exists
// If (target_xml.hasownproperty (nodename )){
// Trace ("-------" + target_xml.child (nodename). (elements (nodename) = nodevalue );
// Return target_xml.child (nodename). (elements (nodename) = nodevalue );
//}
VaR xmllist: xmllist = target_xml.child (nodename );
VaR item: XML;
VaR TMP: string;
For each (item in xmllist ){
TMP = item. tostring ();
If (TMP = nodevalue) return item;
}
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
/**
* Gets the xmllist object of the specified node name, attribute name, and attribute value.
* [Note] only the first subnode under the root node can be used.
*/
Public Function getchildbynodenameandattrname (target_xml: XML, nodename: string,
Attrname: String, attrvalue: string): xmllist {
Try {
// XML. Child (nodename). (@ attrname = 'attrvalue ')
Return target_xml.child (nodename). (attribute (attrname) = attrvalue );
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
//////////////////////////////////////// ////////////////////////////////
/**
* Obtain all the attributes of a node. If the node name is null, obtain the attribute set of the current node.
* [Note] only the first subnode under the root node can be used.
*
*/
Public Function getattrs (target_xml: XML, nodename: String = NULL, position: Int = 0): Array {
Try {
// Return target_xml.child (nodename) [position]. attributes (); // All attribute values are returned.
VaR XML: XML;
If (nodename = NULL ){
// Retrieves the attribute set of the current node
Xml = target_xml;
} Else {
Xml = target_xml.child (nodename) [position];
}
VaR xmllist: xmllist = xml. attributes ();
VaR arr: array = new array ();
VaR tmparr: array = NULL;
For each (XML in xmllist ){
Tmparr = new array ();
Tmparr. Push (XML. Name ());
Tmparr. Push (XML. tostring ());
Arr. Push (tmparr );
}
Return arr;
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
/**
* Obtain the attribute value of the current node.
*/
Public Function getattrvalue (node: XML, attrname: string): String {
Try {
VaR xmllist: xmllist = node. Attribute (attrname );
VaR item: XML;
For each (item in xmllist ){
Break;
}
Return item. tostring ();
} Catch (error: Error ){
Throw error;
}
Return NULL;
}
//////////////////////////////////////// ////////////////////////////////
/**
* Delete A node
* [Note] only the first subnode under the root node can be used.
* Boolean true: Success false: Failure
*/
Public Function deletenodebynodenameandattr (target_xml: XML, nodename: string,
Attrname: String, attrvalue: string): Boolean {
VaR flg: Boolean = false;
Try {
VaR xmllist: xmllist = target_xml.child (nodename). (attribute (attrname) = attrvalue );
For each (var xml: XML in xmllist ){
VaR position: Int = xml. childindex (); // the index of the current node under the parent node
Delete target_xml.child (nodename) [position];
Flg = true;
}
} Catch (error: Error ){
Throw error;
}
Return flg;
}
/**
* Delete A node based on the node name and location.
* [Note] only the first subnode under the root node can be used.
*/
Public Function deletenodebynodenameandposition (target_xml: XML, nodename: string,
Position: INT): Boolean {
Try {
Delete target_xml.child (nodename) [position];
} Catch (error: Error ){
Throw error;
}
Return true;
}
/**
* Delete A node at a specified position
* [Note] only the first subnode under the root node can be used.
* @ Param XML target_xml target XML Object
* @ Param int position
* @ Return Boolean true/false
*/
Public Function deletenodebyposition (target_xml: XML, position: INT): Boolean {
Try {
VaR node: xml = This. getchildbyposition (target_xml, position );
If (node! = NULL ){
VaR nodename: String = This. getnodename (node );
Return this. deletenodebynodenameandposition (target_xml, nodename, position );
}
} Catch (error: Error ){
Throw error;
}
Return false;
}
//////////////////////////////////////// ////////////////////////////////
// Node attribute operation: Modify, add, and delete
Public Function changenodeattr (node: XML, nodename: String, nodevalue: string): XML {
Node. @ [nodename] = nodevalue;
Return node;
}
// Node value operation: Modify
Public Function changenodevalue (node: XML, nodevalue: string): XML {
// Node. = nodevalue;
Return node;
}
//////////////////////////////////////// ////////////////////////////////
//////////////////////////////////////// ////////////////////////////////
//////////////////////////////////////// ////////////////////////////////
} // End class
} // End package
Class singletonenforcer {}