热门文章 | 热门软件| 热门源码 | 热门电影 | 知识库 | 联系我们
软件 源码 教程 影视 健康 招聘
  HTML | JavaScript | ASP | PHP | JSP | NET | VB | VC | VF | Windows | Linux | Mysql | Mssql | Oracle | Struts 
当前位置: 创世纪计算机资源网 -> 文章频道 ->flash 
站内搜索:
将Flash内部的XML转换成对象的类
作者:太极拳 来源:blog 整理日期:2007-9-28

最近写的用于分析Flash内部XML,并转换成对象的类.与大家分享.
用了多次,可以解析较复杂的XML对象.

XmlToArray类代码如下,请保存为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"[img]/images/wink.gif[/img]
   {
    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"[img]/images/wink.gif[/img]
     {
      value [name].push (getValue (node.childNodes [i]));
     } else
     {
      value [name].push (getValue (node [i]));
     }
    } else
    {
     if (type == "xml"[img]/images/wink.gif[/img]
     {
      value [name] = getValue (node.childNodes [i]);
     } else
     {
      value [name] = getValue (node [i]);
     }
    }
   } else
   {
    if (type == "xml"[img]/images/wink.gif[/img]
    {
     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;
 }
}
 
 
用法:
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就是转换出来的对象。

相关文章