comparison src/tools/xml_tools.py @ 289:0e54b1b0a8c8

xml_tools: added ElementParser for raw xml -> domish.Element conversion
author Goffi <goffi@goffi.org>
date Sun, 06 Feb 2011 23:40:29 +0100
parents b1794cbb88e5
children e04ccf122bb6
comparison
equal deleted inserted replaced
288:76247af9917c 289:0e54b1b0a8c8
21 21
22 from logging import debug, info, error 22 from logging import debug, info, error
23 from xml.dom import minidom 23 from xml.dom import minidom
24 from wokkel import data_form 24 from wokkel import data_form
25 import pdb 25 import pdb
26 from twisted.words.xish import domish
26 27
27 """This library help manage XML used in SàT (parameters, registration, etc) """ 28 """This library help manage XML used in SàT (parameters, registration, etc) """
28 29
29 30
30 def dataForm2xml(form): 31 def dataForm2xml(form):
298 self.parentTabsLayout.appendChild(cat) 299 self.parentTabsLayout.appendChild(cat)
299 300
300 def toXml(self): 301 def toXml(self):
301 """return the XML representation of the panel""" 302 """return the XML representation of the panel"""
302 return self.doc.toxml() 303 return self.doc.toxml()
304
305
306
307 class ElementParser(object):
308 """callable class to parse XML string into Element
309 Found at http://stackoverflow.com/questions/2093400/how-to-create-twisted-words-xish-domish-element-entirely-from-raw-xml/2095942#2095942
310 (c) Karl Anderson"""
311
312 def __call__(self, s):
313 self.result = None
314 def onStart(el):
315 self.result = el
316 def onEnd():
317 pass
318 def onElement(el):
319 self.result.addChild(el)
320
321 parser = domish.elementStream()
322 parser.DocumentStartEvent = onStart
323 parser.ElementEvent = onElement
324 parser.DocumentEndEvent = onEnd
325 tmp = domish.Element(("", "s"))
326 tmp.addRawXml(s.replace('\n','').replace('\t',''))
327 parser.parse(tmp.toXml())
328 return self.result.firstChildElement()