如何从XML中的所有子体中删除特定属性?
我有一个类似XML的如何从XML中的所有子体中删除特定属性?,xml,actionscript-3,apache-flex,xml-attribute,Xml,Actionscript 3,Apache Flex,Xml Attribute,我有一个类似XML的 var test:XML = new XML( <record id="5" name="AccountTransactions" <field id="34" type="Nuber"/> </record>); 它仍然显示所有属性:///p>var-xml:xml=new-xml( var xml:XML = new XML( <record id="5" name="AccountTransactions"
var test:XML = new XML( <record id="5" name="AccountTransactions"
<field id="34" type="Nuber"/>
</record>);
它仍然显示所有属性:///p>var-xml:xml=new-xml(
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];
}
);
//生成属性键数组,不包括“id”和“type”
var attributesArray:Array=new Array();
对于每个(var属性:xml.attributes()中的对象)
{
var attributeName:String=attribute.name();
if(attributeName!=“id”&&attributeName!=“type”)
{
属性数组推送(attributeName);
}
}
//循环过滤属性并将其从xml中删除
对于每个(var attributeKey:AttributeArray中的字符串)
{
删除xml@[attributeKey];
删除xml.subjects()@[attributeKey];
}
var测试:XML=newxml(“”);
var属性:XMLList=test.field@*;
变量长度:int=attributes.length();
对于(变量i:int=0;i
您可以发布您的确切xml结构吗?我们可以在不使用循环的情况下执行此操作吗?正如我在上面的代码中尝试的那样。我需要解析一个非常大的XML。。循环需要时间
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];
}
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);