Take this XML example:
<root>
<grandParent GPid="1" hidden="false">
<parent Pid="1" hidden="false">
<child Cid="1" hidden="false"/>
<child Cid="2" hidden="true"/>
</parent>
<parent Pid="2" hidden="false">
<child Cid="3" hidden="false"/>
<child Cid="4" hidden="false"/>
</parent>
</grandParent>
<grandParent GPid="2" hidden="false">
<parent Pid="3" hidden="false">
<child Cid="5" hidden="true"/>
</parent>
<parent Pid="4" hidden="true">
<child Cid="6" hidden="false"/>
</parent>
</grandParent>
<grandParent GPid="3" hidden="true">
<parent Pid="5" hidden="false">
<child Cid="7" hidden="false"/>
</parent>
</grandParent>
</root>
I need some sort of filter to get a copy of this where all the nodes marked "hidden" are removed like so:
<root>
<grandParent GPid="1" hidden="false">
<parent Pid="1" hidden="false">
<child Cid="1" hidden="false"/>
</parent>
<parent Pid="2" hidden="false">
<child Cid="3" hidden="false"/>
<child Cid="4" hidden="false"/>
</parent>
</grandParent>
<grandParent GPid="2" hidden="false">
<parent Pid="3" hidden="false"/>
</grandParent>
</root>
I tried using something like this
var newXML:XML = XML(root.(grandParent.@hidden != "true").(grandParent.parent.@hidden != "true").(grandParent.parent.child.@hidden !=true);
But that really just gives me back the original XML (since I'm asking for the root where those conditions are met I get the root). I understand why my approach doesn't work, but I don't know where to go from here.
You could use a recursive function like this assuming your XML is in a variable myXML. Doing it this way, you would not be tied to the name of your elements (ie. grandParent, parent, child) and you would not be restricted in the number of levels (ie. you could add a <pet>
node to each <child>
node.)
public function removeElements( avXml:XML, avAttributeName:String, avCondition:String) {
var lvAttributeValue:String;
var lvXml:XML;
var lvXmlList:XMLList = new XMLList();
for each( lvXml in avXml.children() ) {
lvAttributeValue = lvXml.attribute( avAttributeName );
if( lvAttributeValue == avCondition )
lvXmlList += lvXml;
avXml.setChildren( lvXmlList );
}
for each( var lvXmlChild:XML in avXml.children() ) {
removeElements(lvXmlChild,avAttributeName,avCondition);
}
}
removeElements(myXML, "hidden", "false");
trace(myXML.toXMLString());
<root hidden="false">
<grandParent GPid="1" hidden="false">
<parent Pid="1" hidden="false">
<child Cid="1" hidden="false"/>
</parent>
<parent Pid="2" hidden="false">
<child Cid="3" hidden="false"/>
<child Cid="4" hidden="false"/>
</parent>
</grandParent>
<grandParent GPid="2" hidden="false">
<parent Pid="3" hidden="false"/>
</grandParent>
</root>
So here's what I was able to come up with, but I don't like having to loop. Let me know if you have a better way:
var newXML:XML = new XML(root);
var i:uint=0;
for(i=0;i<newXML.grandparent.parent.child.(@hidden == false).length();i++){
delete newXML.grandparent.parent.child.(@hidden == false)[0];
//always [0] since the list is shortened by 1 each iteration
}
for(i=0;i<newXML.grandparent.parent.(@hidden == false).length();i++){
delete newXML.grandparent.parent.(@hidden == false)[0];
}
for(i=0;i<newXML.grandparent.(@hidden == false).length();i++){
delete newXML.grandparent.(@hidden == false)[0];
}