2012-02-27 60 views
1

我有這樣如何從XML中的所有後代中刪除特定屬性?

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

我想刪除所有比其他的id屬性和輸入XML的所有節點的XML。通過這段代碼,我無法做到這一點。除了循環之外,你能否提出更好的解決方案?

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

它仍然顯示所有屬性:/

+0

u能烏拉圭回合後確切的XML結構的感謝 – Triode 2012-02-27 07:09:50

回答

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 = [email protected]*; 
    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); 
+0

我們可以做到這一點沒有循環?正如我在上面的代碼嘗試。我需要解析一個非常大的XML ..循環將需要時間 – 2012-02-27 12:46:58

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 [email protected][attributeKey]; 
    delete xml.descendants()[email protected][attributeKey]; 
} 
+0

你爲我工作,謝謝! – 2015-03-04 16:42:28

相關問題