在 Flex 中, 一般将一个 XML 转换成一个对象, 供显示层或其它类使用.
可以根据 XML 标签的属性名, 标签名 及 类的属性名索引 进行转换.
public function convertXMLIntoObject(source:XML, target:Object, convertAttributes:Boolean=true, convertChildren:Boolean=true, convertComplexContent:Boolean=false):Object
{
if ( source && target ) {
var localName:String;
if ( convertAttributes ) {
for each ( var attribute:XML in source.@* ) {
localName = attribute.localName();
if ( target.hasOwnProperty(localName) ) {
try { target[localName] = attribute.toString(); } catch(e:Error) {}
}
}
}
if ( convertChildren ) {
for each ( var child:XML in source.* ) {
localName = child.localName();
if ( target.hasOwnProperty(localName) ) {
if ( child.hasComplexContent() ) {
if ( convertComplexContent ) {
try { target[localName] = child.*.toXMLString(); } catch (e:Error) {}
}
} else {
try { target[localName] = child.toString(); } catch (e:Error) {}
}
}
}
}
}
return target;
}
注意:
上述方法, 只有类的属性是 String 类型, 方可转换, 类的非 String 类型值应在调用该方法以后, 手动进行赋值.
From http://www.jtben.com/document/937020
可以根据 XML 标签的属性名, 标签名 及 类的属性名索引 进行转换.
public function convertXMLIntoObject(source:XML, target:Object, convertAttributes:Boolean=true, convertChildren:Boolean=true, convertComplexContent:Boolean=false):Object
{
if ( source && target ) {
var localName:String;
if ( convertAttributes ) {
for each ( var attribute:XML in source.@* ) {
localName = attribute.localName();
if ( target.hasOwnProperty(localName) ) {
try { target[localName] = attribute.toString(); } catch(e:Error) {}
}
}
}
if ( convertChildren ) {
for each ( var child:XML in source.* ) {
localName = child.localName();
if ( target.hasOwnProperty(localName) ) {
if ( child.hasComplexContent() ) {
if ( convertComplexContent ) {
try { target[localName] = child.*.toXMLString(); } catch (e:Error) {}
}
} else {
try { target[localName] = child.toString(); } catch (e:Error) {}
}
}
}
}
}
return target;
}
注意:
上述方法, 只有类的属性是 String 类型, 方可转换, 类的非 String 类型值应在调用该方法以后, 手动进行赋值.
From http://www.jtben.com/document/937020