Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix single node #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python:
- 2.7
- 3.2
- 3.3
- 3.8
install:
- pip install -r requirements.txt --use-mirrors
script:
Expand Down
35 changes: 27 additions & 8 deletions simplexml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import re
from xml.dom.minidom import getDOMImplementation, parseString

def exclude_attrs_nodes_from_dict(data):
if type(data) == dict:
return {x: data[x] for x in data if x != '_attrs'}
return data

def element_from_dict(document, elRoot, data):

if type(data) == list:
Expand All @@ -30,22 +35,23 @@ def element_from_dict(document, elRoot, data):
if '_attrs' in v:
for name,value in v["_attrs"].items():
elem.setAttribute(name, str(value))
del(v["_attrs"])


if '_value' in v:
value = v.get('_value')
textNode = document.createCDATASection(value) if isinstance(value, str) and re.search("[\<\>\&]", value) else document.createTextNode(str(value))
elem.appendChild(textNode)

else:
element_from_dict(document, elem, v)
element_from_dict(document, elem, exclude_attrs_nodes_from_dict(v))

elRoot.appendChild(elem)

elif isinstance(v, list):
if k.endswith("s"):
elem = document.createElement(k)
for item in v:
elItem = document.createElement(k[0:len(k)-1])
element_from_dict(document, elItem, item)
element_from_dict(document, elItem, exclude_attrs_nodes_from_dict(item))
elem.appendChild(elItem)
elRoot.appendChild(elem)
else:
Expand All @@ -54,14 +60,13 @@ def element_from_dict(document, elRoot, data):
if '_attrs' in item:
for name,value in item["_attrs"].items():
elItem.setAttribute(name, str(value))
del(item["_attrs"])

if '_value' in item:
value = item.get('_value')
textNode = document.createCDATASection(value) if isinstance(value, str) and re.search("[\<\>\&]", value) else document.createTextNode(str(value))
elItem.appendChild(textNode)
else:
element_from_dict(document, elItem, item)
element_from_dict(document, elItem, exclude_attrs_nodes_from_dict(item))

elRoot.appendChild(elItem)

Expand Down Expand Up @@ -126,9 +131,23 @@ def dumps(data):
if type(rootValue) == dict and '_attrs' in rootValue:
for name,value in rootValue["_attrs"].items():
rootNode.setAttribute(name, value)
del(rootValue["_attrs"])

if type(rootValue) == dict and '_value' in rootValue:
# Handles case where root node contains single text node - allowing for attribute definition and CDATA handling
value = rootValue.get('_value')
textNode = document.createCDATASection(value) if isinstance(value, str) and re.search("[\<\>\&]", value) else document.createTextNode(str(value))
rootNode.appendChild(textNode)
# Text or CDATA node cannot have children
return document.toxml()


if type(rootValue) == str:
# Handles case where root node contains single text node - simple case not allowing for attribute definition
text = document.createTextNode(rootValue)
rootNode.appendChild(text)
return document.toxml()

element_from_dict(document, rootNode, rootValue)
element_from_dict(document, rootNode, exclude_attrs_nodes_from_dict(rootValue))

return document.toxml()

Expand Down
19 changes: 19 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,22 @@ def test_can_dumps_with_first_node_list():
response = simplexml.dumps(sometag)

assert '<someTags><someTag><nome>Should Be Nome</nome></someTag><someTag><nome>Should Be Nome</nome></someTag></someTags>' in response

def test_can_dumps_with_single_simple_node():
sometag = {'someTag': 'Should be Name'}
response = simplexml.dumps(sometag)

assert '<someTag>Should be Name</someTag>' in response

def test_can_dumps_with_root_node_containing_text_node():
sometag = {'someTag': {'_attrs': {'id': '1'}, '_value': 'Should be value'}}
response = simplexml.dumps(sometag)

assert '<someTag id="1">Should be value</someTag>' in response

def test_repeated_calls_to_dumps_preserves_dict_attrs():
sometag = {'someTag': {'_attrs': {'id': '1'}, '_value': 'Should be value'}}
response1 = simplexml.dumps(sometag)
response2 = simplexml.dumps(sometag)

assert '<someTag id="1">Should be value</someTag>' in response2
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py26,py27,py33
envlist = py26,py27,py33,py38
[testenv]
deps=
nose==1.1.2
Expand Down