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


Groups > comp.lang.python > #71378

Re: parsing multiple root element XML into text

From Peter Otten <__peter__@web.de>
Subject Re: parsing multiple root element XML into text
Date 2014-05-12 10:22 +0200
Organization None
References <0e5e9a24-3663-4293-a530-239486cf28fc@googlegroups.com> <mailman.9815.1399626164.18130.python-list@python.org> <c7a885fa-11fc-4365-ab19-d70262541f3a@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.9911.1399882939.18130.python-list@python.org> (permalink)

Show all headers | View raw


Percy Tambunan wrote:

> On Friday, May 9, 2014 4:02:42 PM UTC+7, Chris Angelico wrote:
>> On Fri, May 9, 2014 at 6:59 PM, Percy Tambunan <percy.tambunan@gmail.com>
>> wrote:
>> 
>> > Hai, I would like to parse this multiple root element XML
>> 
>> 
>> 
>> Easy fix might be to wrap it in <root> and </root>, which will give
>> 
>> you a new root. Would that help?
>> 
>> 
>> 
>> ChrisA
> 
> Thanks chris for the idea.
> Any suggestion to make it print like this:
> 
> create enumdnsched 4.1.0.1.4.7.3.4.3.2.6.e164.arpa -set naptrFlags=nu
> create enumdnsched 5.1.0.1.4.7.3.4.3.2.6.e164.arpa -set naptrFlags=nu

[Stefan Behnel]

> ElementTree's XMLParser() can be use efficiently for this. Something like
> this should work:
> 
>     from xml.etree.ElementTree import XMLParser
> 
>     parser = XMLParser()
>     parser.feed(b'<root>')
>     parser.feed(real_input_data)
>     parser.feed(b'</root>')
>     root = parser.close()
> 
>     for subtree in root:
>         ...
 
Have you tried to integrate Stefan's example into your script? If so, what 
is the current state of your code, and what problems prevented you from 
completing it?

Expect help to fix these problems, but not necessarily a ready-to-run 
solution.

If you have not made an attempt yet look here for ideas on how you can use 
XPath to extract interesting data from the subtree:

https://docs.python.org/dev/library/xml.etree.elementtree.html#example

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