Apr-15-2018, 11:29 PM
Hello all -
I am parsing some xml files and the hierarchy can be shallow or very deep.
I need to check each level Root -> Parent -> Child -> sub-child -> sub-sub-child...etc to determine if I need to process more/deeper and also save the tag and some of the attributes.
I find that I get the root level and then use a "for child in parent" loop multiple times, like this:
In the end, I need to create a csv that has a single row/line for each item and the items for each child and child type.
thanks for any suggestions or feedback.
I am parsing some xml files and the hierarchy can be shallow or very deep.
I need to check each level Root -> Parent -> Child -> sub-child -> sub-sub-child...etc to determine if I need to process more/deeper and also save the tag and some of the attributes.
I find that I get the root level and then use a "for child in parent" loop multiple times, like this:
doc = etree.parse(full_path)
root = doc.getroot()
# set the namespaces used in the .dtsx file
ns = {'DTS': 'www.microsoft.com/SqlServer/Dts', 'SQLTask': 'www.microsoft.com/sqlserver/dts/tasks/sqltask'}
# collect the executables (parents/children) in an object
executables = root.xpath('DTS:Executables/DTS:Executable', namespaces=ns)
for child0 in executables:
# check each tag or attribute and ONLY continue to process specific types
if child0type = "one of the types i want to process":
#save the child0type tag and one more attributes in some variables and then continue to process...
for child1type in child0:
# check the type and continue
if child1type = "one of the ones I need to process""
# save some info to variables
for child2 in child1:
....
for child3 in child2:
....etc.I am at the point, 6 or 7 levels deep...and it just feels like the wrong way to handle something like this.In the end, I need to create a csv that has a single row/line for each item and the items for each child and child type.
thanks for any suggestions or feedback.
