Serializing data into XML

We'll look at one approach to XML serialization using the built-in libraries. This will build a document from individual tags. A common alternative approach is to use Python introspection to examine and map Python objects and class names to XML tags and attributes.

Here's our XML serialization:

import xml.etree.ElementTree as XMLdef serialize_xml(series: str, data: List[Pair]) -> bytes:    """    >>> data = [Pair(2,3), Pair(5,7)]    >>> serialize_xml( "test", data )    b'<series name="test"><row><x>2</x><y>3</y></row><row><x>5</x><y>7</y></row></series>'    """    doc = XML.Element("series", name=series)    for row in data:        row_xml = XML.SubElement(doc, "row")        x = XML.SubElement(row_xml, "x")        x.text = str(row.x) y = XML.SubElement(row_xml, ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.