2

I have an XML file that has an element called "Node". These can be nested to any depth and the depth of the nesting is not known to me. I need to parse the file and preserve the nesting. For exmaple, if the XML file had:

<Node Name="A">
   <Node Name="B">
      <Node Name="C">
        <Node Name="D">
          <Node Name="E">

When I'm parsing Node "E" I need to know I'm in A/B/C/D/E. Problem is I don't know how deep this can be. This is the code I have so far:

nodes = []

def parseChild(c):
    if c.tag == 'Node':
        if 'Name' in c.attrib: 
            nodes.append(c.attrib['Name'])
        for c1 in c:
            parseChild(c1)
    else:
        for node in nodes:
            print node,
        print c.tag

for parent in tree.getiterator():
    for child in parent:
        for x in child:
            parseChild(x)

My problem is that I don't know when I'm done with a node and I should remove a level of nesting. I would think this is a fairly common situation, but I could not find any examples of parsing a file like this. Perhaps I'm going about it completely wrong.

1 Answer 1

1

With some help from the python list I was able to figure this out. Basically I did this:

  nodes = []

  def process_subtree(c, path):
      name = c.get('Name') if c.tag == 'Node' else None
      if name:
          path.append(name)
          nodes.append('/'.join(path))

      for c1 in c:
          process_subtree(c1, path)

      if name:
          path.pop()

  process_subtree(tree.getroot(), [])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.