From b0896b53b787e098dfd7c47c6179509452218773 Mon Sep 17 00:00:00 2001 From: souliane Date: Sat, 15 Mar 2014 00:15:49 +0100 Subject: [PATCH] tools: xml_tools.ElementParser do not replace '\n' and '\t' with ' ' by default --- src/tools/xml_tools.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/tools/xml_tools.py b/src/tools/xml_tools.py index 13004986..36b6b353 100644 --- a/src/tools/xml_tools.py +++ b/src/tools/xml_tools.py @@ -792,7 +792,11 @@ class ElementParser(object): Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942 (c) Karl Anderson""" - def __call__(self, string): + def __call__(self, string, force_spaces=False): + """ + @param string: the XML string + @param force_spaces: if True, replace occurrences of 'n' and '\t' with ' '. + """ self.result = None def onStart(elem): @@ -809,6 +813,8 @@ class ElementParser(object): parser.ElementEvent = onElement parser.DocumentEndEvent = onEnd tmp = domish.Element((None, "s")) - tmp.addRawXml(string.replace('\n', ' ').replace('\t', ' ')) + if force_spaces: + string = string.replace('\n', ' ').replace('\t', ' ') + tmp.addRawXml(string) parser.parse(tmp.toXml().encode('utf-8')) return self.result.firstChildElement()