Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.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; 'handler': 0.05; '"""': 0.07; 'element': 0.07; 'nested': 0.07; 'parser': 0.07; 'sys': 0.07; 'parsing': 0.09; 'subject:fields': 0.09; 'subject:parsing': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"e"': 0.16; 'jobs.': 0.16; 'name):': 0.16; 'nest': 0.16; 'nesting': 0.16; 'posted.': 0.16; 'preserve': 0.16; 'stuff.': 0.16; 'subject:XML': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'parse': 0.24; 'fairly': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'xml': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '25,': 0.31; 'depth': 0.31; 'node': 0.31; 'file': 0.32; 'class': 0.32; 'problem': 0.35; 'subject:with': 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'nov': 0.38; 'pm,': 0.38; 'skip:p 20': 0.39; 'called': 0.40; 'how': 0.40; 'simple': 0.61; 'email addr:gmail.com': 0.63; 'kind': 0.63; 'skip:n 10': 0.64; 'fact,': 0.69; 'user,': 0.69; 'skip:n 40': 0.81; 'to:none': 0.92; '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:cc :content-type; bh=Yomda0BSBKuRWuQcLx3whNDQuyFXxBBuPaGZJQl8Rcw=; b=g5IyK2lavPDW8V7FN0mdu8qE3w6lNdHFbUKK7FfMobxJv/RX79Lgl2QAg96PAtKIgo JVT9g+RhesYto00iuUkrNOteGFPJ7IxOtfJ1kyQiPtOisf3m4euIbKFbgylbJzRlJgV9 GkrB9URaapGLhshJd6nBL0795XlVPfb63kzwYL+6RLlL1lyH+GpRE8Uxi/qQUm+jT4Dq FZNj1bzIowxG6gtOFhpH9qqH9/cXd8Gxl2a3djNxGWXV+w5kLrQG38DD64h3NlRMzVDj 4a5Ear1YI/r9O4z1tmK9Cya0qPGWgRh29+swvFkh2VGFqCZTiPxqH1UDRevScRXsMt2i en0w== MIME-Version: 1.0 X-Received: by 10.180.187.175 with SMTP id ft15mr18796385wic.20.1385479621956; Tue, 26 Nov 2013 07:27:01 -0800 (PST) In-Reply-To: References: Date: Tue, 26 Nov 2013 10:27:01 -0500 Subject: Re: parsing nested unbounded XML fields with ElementTree From: Neil Cerutti Cc: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385479623 news.xs4all.nl 15902 [2001:888:2000:d::a6]:51629 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60521 On Mon, Nov 25, 2013 at 5:22 PM, Larry.Martell@gmail.com wrote: > 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: I also an ElementTree user, but it's fairly heavy-duty for simple jobs. I use sax for simple those. In fact, I'm kind of a saxophone. This is basically the same idea as others have posted. the_xml = """ """ import io import sys import xml.sax as sax class NodeHandler(sax.handler.ContentHandler): def startDocument(self): self.title = '' self.names = [] def startElement(self, name, attrs): self.process(attrs['Name']) self.names.append(attrs['Name']) def process(self, name): print("Node {} Nest {}".format(name, '/'.join(self.names))) # Do your stuff. def endElement(self, name): self.names.pop() print(sys.version_info) handler = NodeHandler() parser = sax.parse(io.StringIO(the_xml), handler) Output: sys.version_info(major=3, minor=3, micro=2, releaselevel='final', serial=0) Node A Nest Node B Nest A Node C Nest A/B Node D Nest A/B/C Node E Nest A/B/C/D -- Neil Cerutti