Question

i have an XML like

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

i want to remove all the attributes other than id and type in all nodes of XML. by this code i am unable to do this. can you suggest better solution, other than loops.

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

it still show all attributes :/

Was it helpful?

Solution

    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);

OTHER TIPS

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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top