I have strings that have empty xml elements in them, like this:
>>> s = \"\"\"fizz buzz bananas\"\"\"
These strin
You could make your input string a well-formed XML document (with text
as the root element) and parse that into an Element object using fromstring()
. Then append it to the parent.
from lxml import etree as et
s1 = """foo bar parrots like eggs and spam"""
s2 = "{0} ".format(s1)
text = et.fromstring(s2)
root = et.Element('root')
root.append(text)
et.dump(root)
Output:
foo bar parrots like eggs and spam