Turn the lxml.objectify element back into XML

I use lxml.objectify for easy parsing and working with an XML file. For audit reasons, I have to save the derived object along with the source XML of the element.

 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) 

How could I implement turn_elem_into_xml ?

\ edit: why downvote?

+6
source share
2 answers

lxml.etree.tostring

 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>' 
+7
source
Items

lxml.objectify are still normal elements. You can print them like any other, or turn them into lines using lxml.etree.tostring .

+1
source

Source: https://habr.com/ru/post/908164/


All Articles