changeset 905:cad8e52bb2e6

tools: xml_tools.ElementParser do not replace '\n' and '\t' with ' ' by default
author souliane <souliane@mailoo.org>
date Sat, 15 Mar 2014 00:15:49 +0100
parents 95dabdb0c799
children 1cbae66fa725
files src/tools/xml_tools.py
diffstat 1 files changed, 8 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/tools/xml_tools.py	Sat Mar 15 01:19:22 2014 +0100
+++ b/src/tools/xml_tools.py	Sat Mar 15 00:15:49 2014 +0100
@@ -792,7 +792,11 @@
     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 @@
         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()