在AS3中将XML的节点属性值转化为Object中存储和访问
创建XML的时候约定每个节点都有一个ID属性
package cv.resource
{
public class cvXML{
public static function parse(xml:XML,obj:Object):Object{
var results:XMLList = xml.children();
var propertyname:String;
var propertyvalue:*;
for each( var xmlChildren:XML in results)
{
var str:String=xmlChildren.attribute("id");
obj[str]=new Object();
if(xmlChildren.attributes().length()>0)
{//当该节点有属性时
for(var i:int=0;i<xmlChildren.attributes().length();i++)
{
propertyname =xmlChildren.attributes()[i].name();//对应的属性名
propertyvalue =xmlChildren.attributes()[i];//对应的属性值
obj[str][propertyname]=propertyvalue;//将每个属性值对应的存放在OBJECT中
}
}
if(xmlChildren.hasComplexContent())
{ //当子节点仍然包含下一层子节点
parse(xmlChildren,obj[str]); //递归调用
}
}
return obj;
}
}
}