parsing multiple root element XML into text

Peter Otten __peter__ at web.de
Mon May 12 04:22:00 EDT 2014


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 at 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





More information about the Python-list mailing list