lxml question

Stefan Behnel stefan_ml at behnel.de
Fri Oct 3 12:22:05 EDT 2008


Uwe Schmitt wrote:
> I have to parse some text which pretends to be XML. lxml does not want
> to parse it, because it lacks a root element.
> I think that this situation is not unusual, so: is there a way to
> force lxml to parse it ?
> 
> My work around is wrapping the text with "<root>...</root>" before
> feeding lxmls parser.

Yes, you can do that. To avoid creating an intermediate string, you can use
the feed parser and do something like this:

    parser = etree.XMLParser()
    parser.feed("<root>")
    parser.feed(your_xml_tag_sequence_data)
    parser.feed("</root>")
    root = parser.close()

Stefan



More information about the Python-list mailing list