Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'root': 0.05; 'element': 0.07; 'nested': 0.07; 'iterate': 0.09; 'parsing': 0.09; 'path)': 0.09; 'stack.': 0.09; 'subject:fields': 0.09; 'subject:parsing': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"e"': 0.16; '[])': 0.16; 'iterable,': 0.16; 'nesting': 0.16; 'node,': 0.16; 'node.': 0.16; 'nodes': 0.16; 'parent:': 0.16; 'pop()': 0.16; 'preserve': 0.16; 'redundant.': 0.16; 'subject:XML': 0.16; 'wrote:': 0.18; 'result.': 0.19; 'stefan': 0.19; 'examples': 0.20; 'seems': 0.21; '>>>': 0.22; 'putting': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'parse': 0.24; 'tells': 0.24; 'fairly': 0.24; 'helpful': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; 'script': 0.25; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; 'xml': 0.29; 'statement': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'reply.': 0.31; '>>>>': 0.31; 'depth': 0.31; 'end,': 0.31; 'larry': 0.31; 'node': 0.31; 'file': 0.32; 'this.': 0.32; 'thanks!': 0.32; 'up.': 0.33; 'running': 0.33; 'beginning': 0.33; 'could': 0.34; 'problem': 0.35; 'subject:with': 0.35; "can't": 0.35; 'common': 0.35; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'i.e.': 0.36; 'done': 0.36; 'thanks': 0.36; 'should': 0.36; 'list': 0.37; 'level': 0.37; 'nov': 0.38; 'extremely': 0.39; 'called': 0.40; 'how': 0.40; 'remove': 0.60; 'skip:n 30': 0.60; 'then,': 0.60; 'reaching': 0.61; 'entire': 0.61; 'name:': 0.61; 'simply': 0.61; "you're": 0.61; 'times': 0.62; "you'll": 0.62; 'show': 0.63; 'name': 0.63; 'visit': 0.64; '26,': 0.68; 'unclear': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=gT7rDNaoB8trwFcfQNgXcyp6v+inj30vwnTVVLpN/9s=; b=GS5n3lt/6HU6yxg81S9eeKXHLQiCPLgQmhLFzELfVmezwXMCJyObCntzZIJX/7G6MD iZsRLNqterS6J3sLuu11WktSqmhfqyMeOzOAU3Iefm4FgRFmpljt2FtHJr9HKWhqljMC doCWkt/Tbxn2CLt6fRCus30B1n6n7UFt5z8eSzjQDi05r+mP6ZwPsUGrYwBXw67CzlE3 wztwsIEoD4lZZmF6rXCrGL72BVyWI5x29EoI9wWV7rxsD0PGDF89JsFSs7mK27tXOSDd HbFDJtFUMiAVGwKJzyklGSgG3/h2A7ZyXyB5xdCdw8rgc4FZdmeWIwMCYgXhWCz6hi3J gTag== MIME-Version: 1.0 X-Received: by 10.180.90.37 with SMTP id bt5mr3334748wib.43.1385564316675; Wed, 27 Nov 2013 06:58:36 -0800 (PST) In-Reply-To: References: Date: Wed, 27 Nov 2013 09:58:36 -0500 Subject: Re: parsing nested unbounded XML fields with ElementTree From: Larry Martell To: Stefan Behnel Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385564319 news.xs4all.nl 15944 [2001:888:2000:d::a6]:48245 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60613 On Tue, Nov 26, 2013 at 8:20 AM, Stefan Behnel wrote: > Larry Martell, 26.11.2013 13:23: >> On Tue, Nov 26, 2013 at 2:38 AM, Stefan Behnel wrote: >>> Larry.Martell...@gmail.com, 25.11.2013 23:22: >>>> 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: >>>> >>>> >>>> >>>> >>>> >>>> >>>> >>>> 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) >>> >>> This seems hugely redundant. tree.getiterator() already returns a recursive >>> iterable, and then, for each nodes in your document, you are running >>> recursively over its entire subtree. Meaning that you'll visit each node as >>> many times as its depth in the tree. >>> >>> >>>> 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. >>> >>> Your recursive traversal function tells you when you're done. If you drop >>> the getiterator() bit, reaching the end of parseChild() means that you're >>> done with the element and start backing up. So you can simply pass down a >>> list of element names that you append() at the beginning of the function >>> and pop() at the end, i.e. a stack. That list will then always give you the >>> current path from the root node. >> >> Thanks for the reply. How can I remove getiterator()? Then I won't be >> traversing the nodes of the tree. I can't iterate over tree. I am also >> unclear on where to do the pop(). I tried putting it just after the >> recursive call to parseChild() and I tried putting as the very last >> statement in parseChild() - neither one gave the desired result. Can >> you show me in code what you mean? > > untested: > > 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(), []) Thanks! This was extremely helpful and I've use these concepts to write script that successfully parses my file.