Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71181

Re: parsing multiple root element XML into text

Date 2014-05-09 19:52 +0300
From Burak Arslan <burak.arslan@arskom.com.tr>
Subject Re: parsing multiple root element XML into text
References (2 earlier) <87a9arfdha.fsf@elektro.pacujo.net> <87k39vupnc.fsf@dpt-info.u-strasbg.fr> <8738gjf813.fsf@elektro.pacujo.net> <87y4ybdt46.fsf@elektro.pacujo.net> <lkimp0$428$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.9825.1399654376.18130.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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.


Hi Stefan,

Here's a small script:

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

Best,
Burak

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

parsing multiple root element XML into text Percy Tambunan <percy.tambunan@gmail.com> - 2014-05-09 01:59 -0700
  Re: parsing multiple root element XML into text Marko Rauhamaa <marko@pacujo.net> - 2014-05-09 12:01 +0300
  Re: parsing multiple root element XML into text Chris Angelico <rosuav@gmail.com> - 2014-05-09 19:02 +1000
    Re: parsing multiple root element XML into text Percy Tambunan <percy.tambunan@gmail.com> - 2014-05-11 21:12 -0700
      Re: parsing multiple root element XML into text Peter Otten <__peter__@web.de> - 2014-05-12 10:22 +0200
  Re: parsing multiple root element XML into text Stefan Behnel <stefan_ml@behnel.de> - 2014-05-09 11:13 +0200
  Re: parsing multiple root element XML into text Chris Angelico <rosuav@gmail.com> - 2014-05-09 19:15 +1000
  Re: parsing multiple root element XML into text Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-05-09 11:51 +0200
    Re: parsing multiple root element XML into text Marko Rauhamaa <marko@pacujo.net> - 2014-05-09 13:33 +0300
      Re: parsing multiple root element XML into text Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-05-09 14:01 +0200
        Re: parsing multiple root element XML into text Marko Rauhamaa <marko@pacujo.net> - 2014-05-09 15:31 +0300
          Re: parsing multiple root element XML into text Marko Rauhamaa <marko@pacujo.net> - 2014-05-09 15:38 +0300
            Re: parsing multiple root element XML into text Stefan Behnel <stefan_ml@behnel.de> - 2014-05-09 15:55 +0200
              Re: parsing multiple root element XML into text Marko Rauhamaa <marko@pacujo.net> - 2014-05-09 18:29 +0300
            Re: parsing multiple root element XML into text Burak Arslan <burak.arslan@arskom.com.tr> - 2014-05-09 19:52 +0300
            Re: parsing multiple root element XML into text Stefan Behnel <stefan_ml@behnel.de> - 2014-05-09 21:51 +0200
          Re: parsing multiple root element XML into text Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-05-09 17:50 +0200
            Re: parsing multiple root element XML into text Marko Rauhamaa <marko@pacujo.net> - 2014-05-09 19:15 +0300
              Re: parsing multiple root element XML into text Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-05-09 19:16 +0200
                Re: parsing multiple root element XML into text Marko Rauhamaa <marko@pacujo.net> - 2014-05-09 21:04 +0300
                Re: parsing multiple root element XML into text Stefan Behnel <stefan_ml@behnel.de> - 2014-05-09 21:46 +0200

csiph-web