lxml.etree将元素插入到element.text中 [英] lxml.etree insert elements into element.text
问题描述
我的字符串中包含空的xml元素,如下所示:
I have strings that have empty xml elements in them, like this:
>>> s = """fizz buzz <pb n="44"/> bananas"""
这些字符串已使用etree.SubElement
方法分配给xml元素:
These strings have been assigned to xml elements using the etree.SubElement
method:
>>> from lxml import etree as et
>>> root = et.Element('root')
>>> txt = et.SubElement(root, 'text')
>>> txt.text = s
>>> et.dump(root)
<root>
<text>fizz buzz <pb n="44"/> bananas</text>
</root>
在re.split()
和etree的text
和tail
上摆弄一下,我可以在txt.text
中需要的地方插入一个子元素<pb n="44"/>
;但是,有时我在字符串中多次出现<pb/>
元素,这使事情变得复杂:
Fiddling about with re.split()
and etree's text
and tail
I can insert a subelement <pb n="44"/>
where I want it in txt.text
; however, sometimes I've got multiple occurrences of the <pb/>
element in the string, which complicates matters:
>>> s1 = """foo bar <pb n="42"/> parrots like <pb n="43"/> eggs and spam"""
是否有一种简单的方法将这些元素插入到它们属于现有元素text
的位置,而又不会在text
和tail
上摆弄太多?
Is there a straightforward way to insert such elements where they belong in an existing element's text
without fiddling around too much with text
and tail
?
推荐答案
您可以使输入字符串成为格式正确的XML文档(以text
作为根元素),然后使用fromstring()
将其解析为Element对象.然后将其附加到父项.
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 <pb n="42"/> parrots like <pb n="43"/> eggs and spam"""
s2 = "<text>{0}</text>".format(s1)
text = et.fromstring(s2)
root = et.Element('root')
root.append(text)
et.dump(root)
输出:
<root>
<text>foo bar <pb n="42"/> parrots like <pb n="43"/> eggs and spam</text>
</root>
这篇关于lxml.etree将元素插入到element.text中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!