xml - Python ElementTree child node from one tree to another -
i have multiple xml files similar layout.
in following example i've done changes xml tree 1, object 1 , copy changes xml tree 2 , 3:
example xmls:
<example_data> <object class="example" id="xml_tree_1"> <object class="multi_object" id="object_1"> <style many_attributes="stuff_and_changes"/> <object class="object_1" id="random_12"> <style many_attributes="things"/> </object> <object class="object_2" id="random_21"> <style many_attributes="things"/> </object> </object> <object class="object_1" id="object_2"> <style many_attributes="stuff"/> </object> </object> </example_data> <example_data> <object class="example" id="xml_tree_2"> <object class="multi_object" id="object_1"> <style many_attributes="stuff"/> <object class="object_1" id="random_23"> <style many_attributes="stuff"/> </object> <object class="object_2" id="random_32"> <style many_attributes="stuff"/> </object> </object> <object class="object_1" id="object_2"> <style many_attributes="stuff"/> </object> </object> </example_data> <example_data> <object class="example" id="xml_tree_3"> <object class="multi_object" id="object_1"> <style many_attributes="stuff"/> <object class="object_1" id="random_45"> <style many_attributes="stuff"/> </object> <object class="object_2" id="random_54"> <style many_attributes="stuff"/> </object> </object> <object class="object_1" id="object_2"> <style many_attributes="stuff"/> </object> </object> </example_data>
after have done changes 1 tree, how can copy changes other trees also?
i don't know how started.
example python:
import os import xml.etree.elementtree et all_trees = {} def main(): generate_trees("path_to_xmls") copy_style_attributes_to_all_trees("object_1", "xml_tree_1") write_trees() def copy_style_attributes_to_all_trees(object_id, source): key in all_trees: tree = all_trees[key] dest in tree.iter("object"): if (dest.get("id") == object_id): # copy object 1 (and inner objects) style source dest object # have no idea how def generate_trees(directory): root, dirs, files in os.walk(directory): f in files: if f.endswith(".xml"): fpath = os.path.join(root, f) tree = et.parse(fpath) all_trees[fpath] = tree def get_tree(filename): key in all_trees: if (key.find(filename) != -1): return all_trees[key] return none def write_trees(): fpath in all_trees: all_trees[fpath].write(fpath) if __name__ == '__main__': main()
how should done?
Comments
Post a Comment