Python Forum
Parse XML - how to handle deep levels/hierarchy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse XML - how to handle deep levels/hierarchy
#1
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:

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.
Reply


Messages In This Thread
Parse XML - how to handle deep levels/hierarchy - by dwill - Apr-15-2018, 11:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  deep learning python Stevedas 1 1,719 Sep-26-2021, 08:32 AM
Last Post: Yoriz
  How can i generate hierarchy directory Anldra12 2 1,990 Jun-05-2021, 08:28 AM
Last Post: Anldra12
  [split] Teacher (thrown in at the deep end - help) Mr90 2 3,042 May-23-2018, 02:04 PM
Last Post: DeaD_EyE
  Teacher (thrown in at the deep end - help) Mr90 5 3,958 May-22-2018, 01:08 PM
Last Post: DeaD_EyE
  Relative import multiple levels ? Windspar 3 4,481 Feb-02-2018, 11:55 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020