2012-02-27 4 views
1

내가 내가 ID 이외의 모든 속성을 제거하고 XML의 모든 노드를 입력 할XML의 모든 하위 항목에서 특정 속성을 삭제하는 방법은 무엇입니까?

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

같은 XML이있다. 이 코드에 의해 나는 이것을 할 수 없다. 루프가 아닌 더 나은 솔루션을 제안 할 수 있습니까?

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

는 여전히 모든 속성을 보여줍니다/

+0

은 유, 감사 – Triode

답변

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을 구문 분석해야합니다 .. 루프 시간이 걸릴 것입니다 –

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

당신이 나를 위해 일한 UR 정확한 XML 구조 덕분에 게시 할 수 있습니다! –