如何在XML中重命名属性?

时间:2010-07-09 10:01:30

标签: flex actionscript-3

我喜欢XMl

<record id="1" name="CustomerInfo">
    <field name="id" index="1" type="String"/>
</record>

我想将“名称”属性重命名为“匹配”,如

<record id="1" match="CustomerInfo">
    <field match="id" index="1" type="String"/>
</record>

2 个答案:

答案 0 :(得分:2)

您可以根据旧字段添加新字段,然后删除旧字段:

var xml:XML = <record id="1" name="CustomerInfo">
               <field name="id" index="1" type="String"/>
              </record>;

// create new one
xml.field.@match=xml.field.@name;

// delete old one
delete xml.field.@name;

答案 1 :(得分:0)

尝试setName method:我没有使用它,但文档说它也适用于属性。

var xml:XML = <record id="1" name="CustomerInfo">
                 <field name="id" index="1" type="String"/>
              </record>;

xml.@name[0].setName("match");
trace(xml.toXMLString());

xml.field.@name[0].setName("match");
trace(xml.toXMLString());

更新:它适用于Firefox e4x javascript,因此它也适用于ActionScript。试试这个:

var xml:XML = <record id="1" name="CustomerInfo">
                 <field name="id" index="1" type="String"/>
              </record>;

var names:XMLList = xml.descendants("@name");//all `name` attributes 
for(var i:Number = 0; i < names.length(); i++)
{
  names[i].setName("match");
}
trace(xml.toXMLString());