parsing in objectify in python

objectify is a module in the lxml library which enables parsing of XML and HTML documents. Here is an example of how to parse an XML document using objectify in Python:

main.py
from lxml import objectify

# Example XML document
xml_string = '''<root>
                  <person>
                      <name>John</name>
                      <age>30</age>
                      <country>USA</country>
                  </person>
                  <person>
                      <name>Jane</name>
                      <age>25</age>
                      <country>Canada</country>
                  </person>
              </root>
              '''

# Parse the XML document
xml = objectify.fromstring(xml_string)

# Print the names and ages of all people in the document
for person in xml.person:
    print(person.name, person.age)
653 chars
24 lines

In this example, we first import the objectify module from lxml. We then define an example XML document as a string.

To parse the XML document, we use the objectify.fromstring() function, which returns a root element object that we can use to access the contents of the XML document.

Finally, we loop through each person element in the document and print out the values of the name and age elements for each person.

gistlibby LogSnag