My goal is to Recursively create an XML File based on another XML File.
Sample Input Data as below (Original Files are ~ 10MB)
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Picture Name="Template">
<Children>
<Shape Name="GroupLevel" ClassName="Group">
<Properties>
<Property Name="Property1" Value="Value1" />
<Property Name="Property2" Value="Value2" />
<Property Name="Property3" Value="Value3" />
</Properties>
<ContainedObjects>
<Shape Name="Group1" ClassName="Group">
<Properties>
<Property Name="Property1" Value="Value1" />
<Property Name="Property2" Value="Value2" />
<Property Name="Property3" Value="Value3" />
</Properties>
<ContainedObjects>
<Shape Name="Glue123" ClassName="Text" />
<Shape Name="Variable1" ClassName="Variable" />
<Shape Name="Variable2" ClassName="Variable" />
<Shape Name="Variable3" ClassName="Variable" />
<Shape Name="Group2" ClassName="Group">
<Properties>
<Property Name="Property1" Value="Value1" />
<Property Name="Property2" Value="Value2" />
<Property Name="Property3" Value="Value3" />
</Properties>
<ContainedObjects>
<Shape Name="Group3" ClassName="RoundRect" />
</ContainedObjects>
</Shape>
</ContainedObjects>
</Shape>
</ContainedObjects>
</Shape>
</Children>
</Picture>
- Need to Loop Inside
Childtag. During the Loop, if I find theShapetag Whose Parent isChildren, Create a Node with that Tag Name in a blank Root. - Ignore Properties / Property Nodes
- If I find Shape tag Whose Parent is
ContainedObjects, Create a NodeContainedObjectswhile maintaining the depth, add theShapeTag Inside thatContainedObjectsNode. - The actual operations involve creating a Different set of Nodes based on each Shape Tag, Which will be done at a later stage.
For now, I am stuck at the recursive looping.
<Children>
<Shape Name="GroupLevel" ClassName="Group">
<ContainedObjects>
<Shape Name="Group1" ClassName="Group">
<ContainedObjects>
<Shape Name="Glue123" ClassName="Text" />
<Shape Name="Variable1" ClassName="Variable" />
<Shape Name="Variable2" ClassName="Variable" />
<Shape Name="Variable3" ClassName="Variable" />
<Shape Name="Group2" ClassName="Group">
<ContainedObjects>
<Shape Name="Group3" ClassName="RoundRect" />
</ContainedObjects>
</Shape>
</ContainedObjects>
</Shape>
</ContainedObjects>
</Shape>
</Children>
Problem Code - This Doesn't Work, I tried recursively looping, but it doesn't hold the depth/nesting of objects. Please share any pointers.
def RecurseXML(Data_Tree):
for Children in Data_Tree.iterchildren():
if Children.tag == "Shape":
Owner = Children.getparent().tag
Owner = etree.SubElement(Root_Child,Owner)
SomeValue = etree.SubElement(Owner,Children.tag,attrib={"Name":Children.attrib['Name']})
Root_Child.append(SomeValue)
if len(Children) > 0:
RecurseXML(Children)