Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get raw xml as string from Element

Tags:

python

soap

xml

I've the following code which parses an XML response and returns a list of Element objects representing products.

tree =  ET.ElementTree(ET.fromstring(raw_xml_response))
products = root.findall('//Product')

I now need to get the raw XML from each Product so I can store it in a DB. It seems Element doesn't have a method to do this - what's the customary way to get the raw XML from each Product Element?

Thanks,

like image 466
rix Avatar asked Feb 10 '26 04:02

rix


1 Answers

There is a top-level function called tostring (docs) that accepts an element, try

print([ET.tostring(product) for product in products])
like image 105
DeepSpace Avatar answered Feb 13 '26 10:02

DeepSpace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!