@staticmethod
    def meta2xml(meta, filename):
        def dict2elem(elem, d):
            """
            Recursive dictionary element serializer helper.
            """
            if isinstance(d, dict):
                # the element passed is a dict
                for key in d:
                    if isinstance(d[key], list):
                        # this dict item is a list -- serialize a series of these
                        items = d[key]
                        for item in items:
                            sub = etree.SubElement(elem, key)
                            dict2elem(sub, item)
                    else:
                        # this dict item is a scalar or another dict
                        sub = etree.SubElement(elem, key)
                        if isinstance(d[key], basestring):
                            try:
                                d[key] = d[key].encode('ascii', 'replace')
                            except:
                                pass
                            dict2elem(sub,d[key])
            else:
                # the element passed is no longer a dict, it's a scalar value
                elem._setText(str(d).encode('ascii', 'replace'))

            return elem


        elem = objectify.Element('metadata')
        elem = dict2elem(elem, meta)

        # remove some lxml cruft
        objectify.deannotate(elem,pytype=True,xsi=True,xsi_nil=True)
        etree.cleanup_namespaces(elem)

        output = open(filename, 'w+')
        output.write( etree.tostring(elem, pretty_print=True) )
        output.close()
Serialize a metadata dictionary into XML.
dataset/ETHPy150Open PublicMapping/DistrictBuilder/django/publicmapping/redistricting/tasks.py/DistrictShapeFile.meta2xml