I use python and xml.etree.ElementTree for building a xml file. But only the base tree has more than 350 lines and I would like to put this in a seperate file. How do I do that?
My code:
import xml.etree.ElementTree as ET
***
mydata = ET.Element('root_data')
version = ET.SubElement(root_data, 'version')
date = ET.SubElement(root_data, 'date')
groups = ET.SubElement(root_data, 'groups')
group = ET.SubElement(groups, 'group')
templates = ET.SubElement(root_data, 'templates')
template = ET.SubElement(templates, 'template')
tpl_linked = ET.SubElement(template, 'template')
--- a lot more lines like this ---
***
So I would like to have the stuff between the *** in a separate file.
I tried to call it via a function, but that did not work because then the variables were not known in the main program.
xml.etree.ElementTreehave atree.write()function to file, see the documentation.Addition: Assume you have a file
temp.py:This could be imported as other modules, read the modul documentation or packaging for more details. For example you have another python main file, e.g.
xml_file.py:Output: