diff --git a/rest_framework_xml/parsers.py b/rest_framework_xml/parsers.py index 5454356..2951e2a 100644 --- a/rest_framework_xml/parsers.py +++ b/rest_framework_xml/parsers.py @@ -47,7 +47,7 @@ def _xml_convert(self, element): if len(children) == 0: return self._type_convert(element.text) else: - # if the fist child tag is list-item means all children are list-item + # if first child tag is list-item means all children are list-item if children[0].tag == "list-item": data = [] for child in children: @@ -55,7 +55,13 @@ def _xml_convert(self, element): else: data = {} for child in children: - data[child.tag] = self._xml_convert(child) + if child.attrib: + data[child.tag] = { + 'value': self._xml_convert(child), + 'attributes': child.attrib + } + else: + data[child.tag] = self._xml_convert(child) return data diff --git a/tests/test_parsers.py b/tests/test_parsers.py index 38618a8..7922ca2 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -52,6 +52,32 @@ def setUp(self): } ] } + self._attribute_data_input = StringIO( + '' + '' + '121.0' + 'dasd' + '' + '2011-12-25 12:45:00' + '' + ) + self._attribute_data = { + 'field_a': { + 'value': 121, + 'attributes': { + 'name': 'attribute_a' + } + }, + 'field_b': { + 'value': 'dasd', + 'attributes': { + 'id': '0001', + 'name': 'attribute_b' + } + }, + 'field_c': None, + 'field_d': datetime.datetime(2011, 12, 25, 12, 45, 00) + } @skipUnless(etree, 'defusedxml not installed') def test_parse(self): @@ -64,3 +90,9 @@ def test_complex_data_parse(self): parser = XMLParser() data = parser.parse(self._complex_data_input) self.assertEqual(data, self._complex_data) + + @skipUnless(etree, 'defusedxml not installed') + def test_complex_data_parse(self): + parser = XMLParser() + data = parser.parse(self._attribute_data_input) + self.assertEqual(data, self._attribute_data)