Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'elif': 0.05; 'root': 0.05; 'subject:text': 0.05; 'xml,': 0.05; 'element': 0.07; 'processing.': 0.07; 'though:': 0.07; '%s"': 0.09; 'check,': 0.09; 'correct,': 0.09; 'parsing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'subject:parsing': 0.09; 'throws': 0.09; 'url:github': 0.09; 'works.': 0.09; 'api': 0.11; 'ahead,': 0.16; 'burak': 0.16; 'dump': 0.16; 'element.': 0.16; 'ends,': 0.16; 'eof': 0.16; 'expecting': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'gained': 0.16; 'iterator': 0.16; 'none.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:XML': 0.16; 'elements': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'finished': 0.19; 'stack': 0.19; 'stefan': 0.19; 'header:User-Agent:1': 0.23; 'parse': 0.24; 'earlier': 0.24; 'read,': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'xml': 0.29; "i'm": 0.30; 'formed': 0.31; 'post.': 0.31; 'though.': 0.31; 'another': 0.32; 'received:84': 0.35; 'but': 0.35; 'controls': 0.36; 'event,': 0.36; 'next': 0.36; 'possible': 0.36; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'eventually': 0.60; 'break': 0.61; 'lost': 0.61; 'first': 0.61; 'back': 0.62; 'soon': 0.63; 'reads': 0.68; 'events:': 0.84; "it'd": 0.84; 'parser,': 0.84; 'received:arcor-ip.net': 0.84; 'received:pools.arcor-ip.net': 0.84; 'returns.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Stefan Behnel Subject: Re: parsing multiple root element XML into text Date: Fri, 09 May 2014 21:51:56 +0200 References: <0e5e9a24-3663-4293-a530-239486cf28fc@googlegroups.com> <87oaz7uvo4.fsf@dpt-info.u-strasbg.fr> <87a9arfdha.fsf@elektro.pacujo.net> <87k39vupnc.fsf@dpt-info.u-strasbg.fr> <8738gjf813.fsf@elektro.pacujo.net> <87y4ybdt46.fsf@elektro.pacujo.net> <536D07DF.1080800@arskom.com.tr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: dslb-084-056-010-059.pools.arcor-ip.net User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 In-Reply-To: <536D07DF.1080800@arskom.com.tr> 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: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399665133 news.xs4all.nl 2967 [2001:888:2000:d::a6]:46026 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71186 Burak Arslan, 09.05.2014 18:52: > On 05/09/14 16:55, Stefan Behnel wrote: >> ElementTree has gained a nice API in >> Py3.4 that supports this in a much saner way than SAX, using iterators. >> Basically, you just dump in some data that you received and get back an >> iterator over the elements (and their subtrees) that it generated from it. >> Intercept on the right top elements and you get your next subtree as soon >> as it's ready. > > Here's a small script: A bit hard to read, though. > events = etree.iterparse(istr, events=("start", "end")) > stack = deque() > for event, element in events: > if event == "start": > stack.append(element) > elif event == "end": > stack.pop() > > if len(stack) == 0: > break > > print(istr.tell(), "%5s, %4s, %s" % (event, element.tag, element.text)) > > where istr is an input-stream. (Fully working example: > https://gist.github.com/plq/025005a71e8135c46800) > > I was expecting to have istr.tell() return the position where the first > root element ends, which would make it possible to continue parsing with > another call to etree.iterparse(). But istr.tell() returns the position > of EOF after the first call to next() on the iterator it returns. Correct, because it finished parsing. It controls the reading process and reads ahead, that's how iterparse() works. > Without the stack check, the loop eventually throws an exception and the > offset value in that exception is None. > > So I'm lost here, how it'd possible to parse OP's document with lxml? See my earlier post. Instead of XMLParser, just use the XMLPullParser for incremental (non-blocking) parsing and processing. To make this clear, though: to use an XML parser, you need well formed XML, and that means exactly one root element. Stefan