comparison src/tools/xml_tools.py @ 1456:a13be5c22334

xml_tools: minor attribute renaming
author Goffi <goffi@goffi.org>
date Sat, 15 Aug 2015 22:27:39 +0200
parents 8ce9924fa92c
children c005c212b538
comparison
equal deleted inserted replaced
1455:4fb3280c4568 1456:a13be5c22334
1103 class ElementParser(object): 1103 class ElementParser(object):
1104 """callable class to parse XML string into Element 1104 """callable class to parse XML string into Element
1105 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942 1105 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942
1106 (c) Karl Anderson""" 1106 (c) Karl Anderson"""
1107 1107
1108 def __call__(self, string, force_spaces=False): 1108 def __call__(self, raw_xml, force_spaces=False):
1109 """ 1109 """
1110 @param string: the XML string 1110 @param raw_xml(unicode): the raw XML
1111 @param force_spaces: if True, replace occurrences of 'n' and '\t' with ' '. 1111 @param force_spaces: if True, replace occurrences of '\n' and '\t' with ' '.
1112 """ 1112 """
1113 self.result = None 1113 self.result = None
1114 1114
1115 def onStart(elem): 1115 def onStart(elem):
1116 self.result = elem 1116 self.result = elem
1125 parser.DocumentStartEvent = onStart 1125 parser.DocumentStartEvent = onStart
1126 parser.ElementEvent = onElement 1126 parser.ElementEvent = onElement
1127 parser.DocumentEndEvent = onEnd 1127 parser.DocumentEndEvent = onEnd
1128 tmp = domish.Element((None, "s")) 1128 tmp = domish.Element((None, "s"))
1129 if force_spaces: 1129 if force_spaces:
1130 string = string.replace('\n', ' ').replace('\t', ' ') 1130 raw_xml = raw_xml.replace('\n', ' ').replace('\t', ' ')
1131 tmp.addRawXml(string) 1131 tmp.addRawXml(raw_xml)
1132 parser.parse(tmp.toXml().encode('utf-8')) 1132 parser.parse(tmp.toXml().encode('utf-8'))
1133 return self.result.firstChildElement() 1133 return self.result.firstChildElement()