
使用
removexmlElement的方法:
tree=et.fromstring(xml)for bad in tree.xpath("//fruit[@state='rotten']"): bad.getparent().remove(bad) # here I grab the parent of the element to call the remove directly on itprint et.tostring(tree, pretty_print=True, xml_declaration=True)如果我必须与@Acorn版本进行比较,即使要删除的元素不是直接位于xml的根节点下,我的代码也将起作用。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)