7 examples of 'python xml to dict' in Python

Every line of 'python xml to dict' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
20def dict2xml(self, dictionary):
21 return xmltodict.unparse(dictionary, pretty=True)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

136def dict2xml(xmldict, roottag='data', listnames=None, attributenames=[]):
137 return tostring(dict2et(xmldict, roottag, listnames, attributenames))
167def dict2xml(xmldict, filename_or_obj=None, roottag=None):
168 """
169 dict2xml(xmldict, [optional filename or obj], roottag=None)
170
171 Converts a dictionary to an XML ElementTree Element and returns the
172 result. Optionally prints to file if input.
173
174 If roottag is not sent, it is assumed that the dictionary is keyed by
175 the root, and this roottag is gotten with roottag = xmldict.keys()[0]
176
177 If roottag is sent, the root will be created with that name and the
178 input dictionary will be placed under that tag.
179 """
180
181 if not have_element_tree:
182 raise ImportError("Neither cElementTree or ElementTree could "
183 "be imported")
184
185 if roottag is None:
186 keys = list( xmldict.keys() )
187 roottag = keys[0]
188 root = ElementTree.Element(roottag)
189 _dict2xml_recurse(root, xmldict[roottag])
190 else:
191 root = ElementTree.Element(roottag)
192 _dict2xml_recurse(root, xmldict)
193
194 xml_indent(root)
195 # just return the xml if no file is given
196 if filename_or_obj is not None:
197 tree = ElementTree.ElementTree(root)
198 tree.write(filename_or_obj)
199 return root
88def xml2dict(root, dictclass=XmlDictObject, seproot=False, noroot=False):
89 """
90
91 d=xml2dict(element or filename, dictclass=XmlDictObject,
92 noroot=False, seproot=False)
93
94 Converts an XML file or ElementTree Element to a dictionary
95
96 If noroot=True then the root tag is not included in the dictionary and
97 xmldict[roottag]
98 is returned.
99
100 If seproot=True then the root tag is not included in the dictionary, and
101 instead the tuple
102 (xmldict[roottag], roottag)
103 is returned. The name of the roottag is lost in this case.
104 """
105
106 if not have_element_tree:
107 raise ImportError("Neither cElementTree or ElementTree could "
108 "be imported")
109
110 # If a string is passed in, try to open it as a file
111 if type(root) == type(''):
112 root = ElementTree.parse(root).getroot()
113 elif not isinstance(root, ElementTree.Element):
114 raise TypeError('Expected ElementTree.Element or file path string')
115
116 xmldict = dictclass({root.tag: _xml2dict_recurse(root, dictclass)})
117
118 keys = list( xmldict.keys() )
119 roottag = keys[0]
120 if seproot:
121 return xmldict[roottag], roottag
122 elif noroot:
123 return xmldict[roottag]
124 else:
125 return xmldict
177def dict2xmlPollRequest(dict):
178 raise Exception('This function is not implemented - Use an HTTP GET!')
23def etree_to_dict(tree):
24 """Translate etree into dictionary.
25
26 :param tree: etree dictionary object
27 :type tree: <http://lxml.de/api/lxml.etree-module.html>
28
29 """
30 d = {tree.tag.split('}')[1]: map(
31 etree_to_dict, tree.iterchildren()
32 ) or tree.text}
33
34 return d
64def test_xml_str_to_dict(self):
65 xml = (
66 '<a foo="bar" faa="bor"></a>',
67 '<a>text</a>',
68 '<a><sub>a</sub></a>',
69 '<a><sub>foo</sub><sub>bar</sub></a>',
70 )
71
72 dicts = (
73 {'a': {'foo': 'bar', 'faa': 'bor'}},
74 {'a': {'_content': 'text'}},
75 {'a': {'sub': {'_content': 'a'}}},
76 {'a': {'sub': [{'_content': 'foo'}, {'_content': 'bar'}]}},
77
78 )
79 for i in range(len(xml)):
80 self.assertEqual(
81 utils.xml_str_to_dict(xml[i]),
82 dicts[i])

Related snippets