objectify.fromstring to list in python

To convert an XML string to a list of objects using objectify.fromstring() method in Python with lxml module, follow these steps:

  1. Import the objectify class from the lxml library.
  2. Call the objectify.fromstring() method and pass it the XML string as an argument.
  3. Convert the resulting object into a list using the list() function.

Here's the code:

main.py
from lxml import objectify

xml_string = '<root><item><name>apple</name></item><item><name>banana</name></item></root>'

xml_obj = objectify.fromstring(xml_string)

items_list = list(xml_obj.item)

print(items_list)
216 chars
10 lines

Output:

main.py
[<Element item at 0x7f2a0b43e548>, <Element item at 0x7f2a0b43e608>]
69 chars
2 lines

gistlibby LogSnag