I need to completely remove elements, based on the contents of an attribute, using python's lxml. Example:
import lxml.etree as et
xml="""
<groceries>
<fruit state="rotten">apple</fruit>
<fruit state="fresh">pear</fruit>
<fruit state="fresh">starfruit</fruit>
<fruit state="rotten">mango</fruit>
<fruit state="fresh">peach</fruit>
</groceries>
"""
tree=et.fromstring(xml)
for bad in tree.xpath("//fruit[@state='rotten']"):
#remove this element from the tree
print et.tostring(tree, pretty_print=True)
I would like this to print:
<groceries>
<fruit state="fresh">pear</fruit>
<fruit state="fresh">starfruit</fruit>
<fruit state="fresh">peach</fruit>
</groceries>
Is there a way to do this without storing a temporary variable and printing to it manually, as:
newxml="<groceries>
"
for elt in tree.xpath('//fruit[@state='fresh']'):
newxml+=et.tostring(elt)
newxml+="</groceries>"
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…