AS3将XML转成对象的类

前端之家收集整理的这篇文章主要介绍了AS3将XML转成对象的类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

完整代码下载

package com.rofine.util.reflection
{
	import com.degrafa.geometry.utilities.ArcUtils;
	import com.rofine.util.reflection.*;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import mx.collections.ArrayCollection;
	import mx.formatters.DateFormatter;
	import mx.utils.ObjectUtil;

	public  class XMLConverter
	{
		private static var A_CLASS:String="mx.collections::Array";
		private static var C_CLASS:String="mx.collections::ArrayCollection";
		private static var O_CLASS:String="Object";
		private static var classinfos:Object=new Object();
		private static var baseTypes:Array=["String","int","Number","uint","Boolean","Date"];
		private static function ToObject(xml:Object,type:String,mapping:Object=null):Object
		{
			if(xml!=null)
			{
				switch(type)
				{
					case "String": return xml.toString(); break;
					case "int":
				    case "Number":
					case "uint": return Number(xml.toString()); break;
					case "Boolean": return Boolean(xml.toString());break;
					case "Date":return DateFormatter.parseDateString(xml.toString()); break;
					default:break;
				}
				var cls:Class = flash.utils.getDefinitionByName(type) as Class;
				return fromXMLToObject(xml,cls,mapping);
			}
			return null;
		}
		///获取需要赋值反射类型属性和变量
		private static function getAttrinfos(cls:Class):Array
		{
			var className:String=getQualifiedClassName(cls);
			if(classinfos[className]==null)
			{
				var classInfo:ClassInfo=new ClassInfo(cls);
				var attrs:Array=new Array();
				for each(var  propertyInfo:PropertyInfo in classInfo.properties)
				{
					if(propertyInfo.access==PropertyInfo.ACCESS_READWRITE || propertyInfo.access==PropertyInfo.ACCESS_WRITEONLY)
					{
						attrs.push(new VariableInfo(propertyInfo.name,propertyInfo.type));
					}
				}
				for each(var varinfo:VariableInfo in classInfo.variables)
				{
					attrs.push(new VariableInfo(varinfo.name,varinfo.type));
				}
				classinfos[className]=attrs;
			}
			return classinfos[className] as Array;
		}
		private static function getAttrObject(xml:XML,attr:VariableInfo,mapping:Object):Object
		{
			if(baseTypes.indexOf(attr.type)==-1)
			{
				if(mapping!=null)
				{
					var isObj:Boolean=attr.type==O_CLASS;
					if(isObj || attr.type==A_CLASS || attr.type==C_CLASS)
					{
						if(xml[attr.name]!=null && xml[attr.name][0]!=null)
						{
							var children:XMLList=xml[attr.name][0].children();
							var list:ArrayCollection= new ArrayCollection();
							var atteObj:Object=new Object();
							for each(var child:XML in children)
							{
								var name:String=child.name().localName;
								var obj:Object=fromXMLToObject(child,mapping[name] as Class,mapping);
								if(obj!=null)
								{
									if(isObj)
									{
										atteObj[name]=obj;
									}
									else
									{
										list.addItem(obj);
									}
								}
							}
						}
						if(isObj)
						{
							return atteObj;
						}
						else
						{
							return attr.type==A_CLASS?list.toArray():list;
						}
					}
				}
			}
			else
			{
				var value:Object=ToObject(xml[attr.name],attr.type);
				value=(value==null?ToObject(xml.@[attr.name],attr.type):value);
				return value;
			}
			return null;
		}
		///通过XML对象或者XMLList对象返回对象
		public static function fromXMLToObject(xml:*,cls:Class,mapping:Object=null):Object
		{
			var xmlList:XMLList=xml is XML?XMLList(xml):XMLList((xml as XMLList)[0]);
			if(xmlList.length()==0 || cls==null) return null;
			var list:ArrayCollection=fromXMLToList(xmlList,mapping);
			return list.length==0?null:list[0];
		}
		///通过XMLList返回ArrayCollection对象
		public static function fromXMLToList(xmlList:XMLList,mapping:Object=null):ArrayCollection
		{
			if(xmlList==null) return null;
			if(xmlList.length()==0) return null;
			if(cls==null) return null;
			var list:ArrayCollection=new ArrayCollection();
			var attrs:Array=getAttrinfos(cls);
			for each (var xml : XML in xmlList)
			{
				var obj:Object=new cls();
				for each(var attr:VariableInfo in attrs)
				{
					obj[attr.name]=getAttrObject(xml,attr,mapping);
				}
				list.addItem(obj);
			}
			return list;
		}
	}
}
原文链接:https://www.f2er.com/xml/300289.html

猜你在找的XML相关文章