[Tutor] module to parse XMLish text?

Stefan Behnel stefan_ml at behnel.de
Sat Jan 15 07:53:33 CET 2011


Wayne Werner, 15.01.2011 03:25:
> On Fri, Jan 14, 2011 at 4:42 PM, Terry Carroll wrote:
>> On Fri, 14 Jan 2011, Karim wrote:
>>
>>   from xml.etree.ElementTree import ElementTree
>>
>> I don't think straight XML parsing will work on this, as it's not valid
>> XML; it just looks XML-like enough to cause confusion.
>
> It's worth trying out - most (good) parsers "do the right thing" even when
> they don't have strictly valid code. I don't know if xml.etree is one, but
> I'm fairly sure both lxml and BeautifulSoup would probably parse it
> correctly.

They wouldn't. For the first tags, the text values would either not come 
out at all or they would be read as attributes and thus loose their order 
and potentially their whitespace as well. The other tags would likely get 
parsed properly, but the parser may end up nesting them as it hasn't found 
a closing tag for the previous tags yet.

So, in any case, you'd end up with data loss and/or a structure that would 
be much harder to handle than the (relatively) simple file structure.

Stefan



More information about the Tutor mailing list