Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
XML2Dict Patch for handling empty text, returned as None. NoneType has no String Methods
diff --git a/encoder.py b/encoder.py
index 9b4dc41..f5c1829 100644
--- a/encoder.py
+++ b/encoder.py
@@ -34,9 +34,12 @@ class XML2Dict(object):
ctag = child.tag
cattr = child.attrib
ctree = self._parse_node(child)
+ ctext = child.text
+ if ctext:
+ ctext = ctext.strip().encode(self._coding)
if not ctree:
- cdict = self._make_dict(ctag, child.text.strip().encode(self._coding), cattr)
+ cdict = self._make_dict(ctag, ctext, cattr)
else:
cdict = self._make_dict(ctag, ctree, cattr)
@refnode

This comment has been minimized.

Copy link
Owner Author

refnode commented Sep 11, 2011

Better import for elementtree

try:
import cElementTree as etree
except ImportError:
try:
import xml.etree.ElementTree as etree
except ImportError:
from elementtree import ElementTree as etree

@mcspring

This comment has been minimized.

Copy link

mcspring commented Sep 13, 2011

Thanks for your path, I have just update the package follow your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.