def _append_element(self, field_name, data, node=self._root.tag): if(node==self._root.tag): self._root.append(ElementTree.Element(field,attrib={text:data})) else: for field in root.iter(node): field.append(ElementTree.Element(field,attrib={text:data})) self._tree.write('xml_renderer.xml')
def ConvertDictToXml(xmldict): """ Converts a dictionary to an XML ElementTree Element """ roottag = xmldict.keys()[0] root = ElementTree.Element(roottag) _ConvertDictToXmlRecurse(root, xmldict[roottag]) return root
def dict_to_xml(obj): """ Return a xml representation of the given dictionary: 1. keys of the dictionary become sublements. 2. if a value is a list, then key is a set of sublements. 3. keys starting with '@' became an attribute. {'duck': {'birth_date': '1934-06-04T00:00:00', 'created_by': {'@href': 'http://en.wikipedia.org/wiki/Walt_Disney', 'cryopreserved': True, 'name': 'Walt Disney'}, 'family': {'nephew': [{'name': 'Huey'}, {'name': 'Dewey'}, {'name': 'Louie'}], 'children': [], 'uncles': {'uncle': [{'name': 'Scrooge McDuck'}, {'name': 'Ludwig Von Drake'}]}}, 'first_film': None, 'last_film': None, 'name': 'Donald', 'species': {'@href': 'http://en.wikipedia.org/wiki/Pekin_duck'}} } <?xml version="1.0" encoding="UTF-8"?> <duck> <name>Donald</name> <family> <children /> <nephew><name>Huey</name></nephew> <nephew><name>Dewey</name></nephew> <nephew><name>Louie</name></nephew> <uncles> <uncle><name>Scrooge McDuck</name></uncle> <uncle><name>Ludwig Von Drake</name></uncle> </uncles> </family> <last_film /> <first_film /> <created_by href="http://en.wikipedia.org/wiki/Walt_Disney"> <cryopreserved>True</cryopreserved> <name>Walt Disney</name> </created_by> <birth_date>1934-06-04T00:00:00</birth_date> <species href="http://en.wikipedia.org/wiki/Pekin_duck" /> </duck> """ if not obj: return # top level dictionary must contain a single entry # corresponding to the root element key, value = obj.popitem() root = etree.Element(key) element_for_value(value, root) return (b'<?xml version="1.0" encoding="UTF-8"?>' + etree.tostring(root, encoding='utf-8'))
def _ConvertDictToXmlRecurse(parent, dictitem): assert not isinstance(dictitem, list) if isinstance(dictitem, dict): for (tag, child) in dictitem.iteritems(): if str(tag) == '_text': parent.text = str(child) elif isinstance(child, list): # iterate through the array and convert for listchild in child: elem = ElementTree.Element(tag) parent.append(elem) _ConvertDictToXmlRecurse(elem, listchild) else: elem = ElementTree.Element(tag) parent.append(elem) _ConvertDictToXmlRecurse(elem, child) else: parent.text = str(dictitem)
def write(self, data): if self.options.set is None and self.options.param is None: raise IOError("Unspecified model component") root = ET.Element('table') table = self.get_table() labels = table[0] for i in range(len(labels)): labels[i] = str(labels[i]) for trow in table[1:]: row = ET.SubElement(root, 'row') for i in range(len(labels)): data = ET.SubElement(row, labels[i]) data.set('value', str(trow[i])) # tree = ET.ElementTree(root) tree.write(self.filename)