1

私は次のようなXMLを持っています

var test:XML = new XML( <record id="5" name="AccountTransactions"
    <field id="34" type="Nuber"/>
    </record>);

id以外のすべての属性を削除し、XMLのすべてのノードに入力したいと思います。このコードでは、これを行うことはできません。ループ以外のより良い解決策を提案できますか?

var atts:XMLListCollection = new XMLListCollection(test.descendants().attributes().((localName() != "id") && (localName() != "type")));
atts.removeAll(); trace(test)

それでもすべての属性が表示されます:/

4

2 に答える 2

1
var xml:XML = new XML(
    <record id="5" name="AccountTransactions">
        <field id="34" type="Number">
            <test id="0"/>
        </field>
    </record>);

//make array of attribute keys, excluding "id" and "type"
var attributesArray:Array = new Array();
for each (var attribute:Object in xml.attributes())
{
    var attributeName:String = attribute.name();
    if (attributeName != "id" && attributeName != "type")
    {
        attributesArray.push(attributeName);
    }
}

//loop through filtered attributes and remove them from the xml
for each (var attributeKey:String in attributesArray)
{
    delete xml.@[attributeKey];
    delete xml.descendants().@[attributeKey];
}
于 2012-02-27T07:12:19.340 に答える
1
    var test:XML = new XML( '<record id="5" name="AccountTransactions"><field id="34" type="Nuber" score="ded"/><field id="35" type="Nuber" score="sc"/></record>');
    var attributes:XMLList = test.field.@*;
    var length:int = attributes.length();
    for (var i:int = 0; i < length; i++) {
        (attributes[i].localName() != "id" && attributes[i].localName() != "type") ? [delete attributes[i], length--] : void;
    }
    trace(test);
于 2012-02-27T07:31:06.307 に答える