How to rename an attribute in XML?
i have XMl like
<record id="1" name="CustomerInfo">
<field开发者_JAVA百科 name="id" index="1" type="String"/>
</record>
i want to rename "name" attribute to "match" like
<record id="1" match="CustomerInfo">
<field match="id" index="1" type="String"/>
</record>
You can add a new field based on the old one and then delete the old :
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;
Try setName method: I haven't used it, but the docs says it will work on attributes too.
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());
Update: It works in Firefox e4x javascript, so it should work in ActionScript too. Try this:
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());
精彩评论