diff --git a/.gitignore b/.gitignore index d2d6f36..f180777 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ nosetests.xml .mr.developer.cfg .project .pydevproject +.idea \ No newline at end of file diff --git a/simplexml/core.py b/simplexml/core.py index de8af95..3a40140 100644 --- a/simplexml/core.py +++ b/simplexml/core.py @@ -86,6 +86,20 @@ def isNodeList(elem): return False return True +def dict_from_attributes(attributes, nodeValue): + if len(attributes.keys()): + dic = { + '_attrs' : {}, + } + for a, v in attributes.items(): + dic['_attrs'][a] = v + if nodeValue: + dic['_value'] = nodeValue + return dic + else: + return nodeValue + + def dict_from_element(element, dic): if element.hasChildNodes(): @@ -99,12 +113,12 @@ def dict_from_element(element, dic): else: for node in element.childNodes: if node.nodeType == node.TEXT_NODE: - dic[element.nodeName] = node.nodeValue + dic[element.nodeName] = dict_from_attributes(element.attributes, node.nodeValue) elif node.nodeType == node.CDATA_SECTION_NODE: - dic = node.nodeValue + dic = dict_from_attributes(element.attributes, node.nodeValue) else: if node.hasChildNodes and len(node.childNodes) == 1 and node.childNodes[0].nodeType == node.TEXT_NODE: - dic[node.nodeName] = node.childNodes[0].nodeValue + dic[node.nodeName] = dict_from_attributes(node.attributes, node.childNodes[0].nodeValue) else: if node.nodeName in dic: if type(dic[node.nodeName]) != type([]): @@ -112,6 +126,8 @@ def dict_from_element(element, dic): dic[node.nodeName].append(dict_from_element(node, {})) else: dic[node.nodeName] = dict_from_element(node, {}) + elif len(element.attributes.keys()): + dic[element.nodeName] = dict_from_attributes(element.attributes, None) return dic diff --git a/tests/test_core.py b/tests/test_core.py index d0fc2fa..f10882f 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -18,6 +18,20 @@ def test_can_loads(): assert type(response['someTag']['itens']) == list assert response['someTag']['itens'][0]['type'] == 'Should Be Type item1' +def test_can_loads_attributes(): + + docxml = u'value3' + response = simplexml.loads(docxml) + + assert type(response['tags']) == list + assert type(response['tags'][0]) == dict + + assert response['tags'][0]['tag']['_attrs']['attr'] == 'should be tag value' + assert response['tags'][1]['tag']['_attrs']['attr'] == 'value2' + assert response['tags'][1]['tag']['_value'] == 'value3' + + final_docxml = simplexml.dumps(response) + assert docxml == final_docxml def test_can_loads_list_plural(): @@ -52,7 +66,6 @@ def test_can_dumps(): assert 'Should be name' in response assert '90' in response - def test_can_dumps_diff_cdata(): sometag = {'someTag': {'value': 'hello world', 'scaped': 'Should Be bold'}} @@ -86,10 +99,9 @@ def test_can_dumps_with_list_non_plural(): def test_can_dumps_with_list_non_plural_with_attrs(): - sometag = {'someTag': {'name': 'Should be name', 'child': {'id': 90}, 'item': [{'_attrs': {'attr': 'value'}, 'type': 'Should Be Type item1'}, {'type': 'Should Be Type item2'}]}} + sometag = {'someTag': {'name': 'Should be name', 'child': {'id': 90}, 'item': [{'_attrs': {'attr': 'value'}, '_value':'item value'}, {'type': 'Should Be Type item2'}]}} response = simplexml.dumps(sometag) - - assert 'Should Be Type item1Should Be Type item2' in response + assert 'item valueShould Be Type item2' in response def test_can_dumps_with_node_value():