Here's how to parse from text string to lxml structured data type.
Python 2:
from lxml import etree xml_str = "<parent><child>text</child><child>other text</child></parent>" root = etree.fromstring(xml_str) print etree.tostring(root, pretty_print=True)
Python 3:
from lxml import etree xml_str = "<parent><child>text</child><child>other text</child></parent>" root = etree.fromstring(xml_str) print(etree.tostring(root, pretty_print=True).decode())
Outputs:
<parent> <child>text</child> <child>other text</child> </parent>
monkut
source share