comparison src/tools/xml_tools.py @ 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 65bf1bc70f6b
children 9754c0ebadba
comparison
equal deleted inserted replaced
904:95dabdb0c799 905:cad8e52bb2e6
790 class ElementParser(object): 790 class ElementParser(object):
791 """callable class to parse XML string into Element 791 """callable class to parse XML string into Element
792 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942 792 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942
793 (c) Karl Anderson""" 793 (c) Karl Anderson"""
794 794
795 def __call__(self, string): 795 def __call__(self, string, force_spaces=False):
796 """
797 @param string: the XML string
798 @param force_spaces: if True, replace occurrences of 'n' and '\t' with ' '.
799 """
796 self.result = None 800 self.result = None
797 801
798 def onStart(elem): 802 def onStart(elem):
799 self.result = elem 803 self.result = elem
800 804
807 parser = domish.elementStream() 811 parser = domish.elementStream()
808 parser.DocumentStartEvent = onStart 812 parser.DocumentStartEvent = onStart
809 parser.ElementEvent = onElement 813 parser.ElementEvent = onElement
810 parser.DocumentEndEvent = onEnd 814 parser.DocumentEndEvent = onEnd
811 tmp = domish.Element((None, "s")) 815 tmp = domish.Element((None, "s"))
812 tmp.addRawXml(string.replace('\n', ' ').replace('\t', ' ')) 816 if force_spaces:
817 string = string.replace('\n', ' ').replace('\t', ' ')
818 tmp.addRawXml(string)
813 parser.parse(tmp.toXml().encode('utf-8')) 819 parser.parse(tmp.toXml().encode('utf-8'))
814 return self.result.firstChildElement() 820 return self.result.firstChildElement()