June 2019
Beginner to intermediate
770 pages
19h 24m
English
Loading Python objects from an XML document is a two-step process. First, we need to parse the XML text to create the document objects. Then, we need to examine the document objects to produce Python objects. As noted previously, the tremendous flexibility of XML notation means that there isn't a single XML-to-Python serialization.
One approach to walk through an XML document involves making XPath-like queries to locate the various elements that were parsed. Here's a function to walk through an XML document, emitting the Blog and Post objects from the available XML:
def build_blog(document: XML.ElementTree) -> Blog_X: xml_blog = document.getroot() blog = Blog_X(xml_blog.findtext("title")) for xml_post in xml_blog.findall( ...
Read now
Unlock full access