我使用 lxml.objectify
轻松解析和处理 XML 文件。出于审计原因,我必须将派生对象与元素的原始 XML 代码一起保存。
root = lxml.objectify.fromstring(self.get_xml_data())
for i, elem in enumerate(root.elements):
# create new database entry based on elem
elem_obj.source_code = turn_elem_into_xml(elem)
我如何实现 turn_elem_into_xml
?
最佳答案
In [21]: r = lxml.objectify.fromstring('<root><item>1</item><item>2</item></root>')
In [22]: lxml.etree.tostring(r.item)
Out[22]: '<item>1</item>'
关于python - 将 lxml.objectify 的元素转回 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9231515/