package uqee.core.utils
{
import flash.display.DisplayObject;
import flash.utils.describeType;
import flash.utils.getTimer;
public final class XmlUtils
{
/**将xml对象转变成为一个类对象*/
public static function XmlToObject( obj:Object, node:XML ):void {
var attrs:XMLList = node.attributes();
for(var i:int = 0; i < attrs.length(); i++)
{
var prop:String = attrs[i].localName();
if( obj.hasOwnProperty(prop) ) {
if( obj[prop] is Boolean ) {
obj[prop] = attrs[i].toString()=="1";
} else {
obj[prop] = attrs[i];
}
}
}
}
/**将一个object对象转换成xml*/
public static function ObjectToXml( obj:Object, node:XML ):void {
var xml:XML = describeType( obj );
var list:XMLList = xml.variable;
for each( var prop:XML in list ) {
var n:String = prop.@name.toString();
switch( prop.@type.toString().toLowerCase() ) {
case "boolean":
node.@[n] = obj[n] ? 1 : 0;
break;
case "int":
case "number":
case "uint":
case "string":
node.@[n] = obj[n];
break;
}
}
list = xml.accessor.(@access=="readwrite");
for each( var prop2:XML in list ) {
var n2:String = prop2.@name.toString();
switch( prop2.@type.toString().toLowerCase() ) {
case "boolean":
node.@[n2] = obj[n2] ? 1 : 0;
break;
case "int":
case "number":
case "uint":
case "string":
node.@[n2] = obj[n2];
break;
}
}
}
/**获取一个xml文件里面的一个节点
* @param fileName xml文件名称
* @param tagName 节点名称
* @param ident 节点中属性attr对应的值
* @param attr 属性名称
* */
public static function GetNode( fileName:String, tagName:String, ident:*, attr:String = "ident" ):XML
{
var xml:XML = ResourceReader.GetXml( fileName );
if ( xml )
{
var list:XMLList = xml[tagName].(@[attr]==ident);
if ( list.length()>0 )
{
return list[0];
}
}
return null;
}
/**
* 返回xml中的一个xmllist
* @param fileName xml文件名称
* @param tagName 子节点名称
* @param subTagName 子节点的子节点名称
* @param ident 属性参考值
* @param attr 节点属性参考值
* */
public static function GetNodeList( fileName:String, tagName:String, subTagName:String = "", ident:* = -1, attr:String = "ident" ):XMLList
{
var xml:XML = ResourceReader.GetXml( fileName );
if ( xml )
{
var list:XMLList;
if ( subTagName )
{
list = xml[tagName][subTagName];
}
else
{
list = xml[tagName];
}
if ( ident==-1 )
{
return list;
}
return list.(@[attr]==ident);
}
return new XMLList();
}
/**
* 如果有URL则返回相应URL,否则通过id取对象<br/>
* 示例:<br/>
* GetXmlImage( node, "icon" );<br/>
* 如果 node=<Object icon="0.gif" />;<br/>
* 则返回 "resource/Image/0.gif";<br/>
* 如果 node=<Object iconId="1001" />;<br/>
* 则返回 ResourceReader.GetImageByIdent( iconId );
*/
public static function GetXmlImage( node:XML, attr:String = "icon", className:String = null ):Object
{
if ( node==null )
{
return null;
}
var val:String = node.@[attr];
if ( val )
{
var obj:DisplayObject = ResourceReader.GetImage( val );
if( obj ) {
return obj;
}
return "resource/Image/" + val;
}
if ( !className )
{
className = node.@[attr+"Class"];
}
var ident:int = node.@[attr+"Id"];
if ( ident>0 )
{
var flip:Boolean = node.@flip=="1";
return ResourceReader.GetImageByIdent( ident, className, flip );
}
if ( className )
{
return ClassLoader.GetInstance( className );
}
return null;
}
/**
* 如果有字符则返回相应内容,否则通过id取对象<br/>
* 示例:<br/>
* GetXmlText( node, "name" );<br/>
* 如果 node=<Object name="天水" />;<br/>
* 则返回 "天水";<br/>
* 如果 node=<Object nameId="1001" />;<br/>
* 则返回 ReaderFactory.TextReader.GetText( nameId );
*/
public static function GetXmlText( node:XML, attr:String = "name" ):String
{
var val:String = node.@[attr];
if ( val )
{
return val;
}
var ident:String = node.@[attr+"Id"];
if ( ident )
{
return ResourceReader.GetTextCont( ident );
}
return "";
}
/**
* 根据名称关键字搜索符合条件的物品列表
* @param filename 文件名
* @param tagname 标签项
* @param attr 属性项
* @param key 搜索关键字
* @return
*
*/
public static function GetMatchKeyList(filename:String, tagname:String, attr:String, key:String):XMLList{
var xmllist:XMLList = ResourceReader.GetXml(filename)[tagname].(@[attr].search(key) != -1)
return xmllist;
}
}
}