xml_tools: added ElementParser for raw xml -> domish.Element conversion
This commit is contained in:
parent
b91973e803
commit
ead51b4fec
1 changed files with 26 additions and 0 deletions
|
|
@ -23,6 +23,7 @@ from logging import debug, info, error
|
|||
from xml.dom import minidom
|
||||
from wokkel import data_form
|
||||
import pdb
|
||||
from twisted.words.xish import domish
|
||||
|
||||
"""This library help manage XML used in SàT (parameters, registration, etc) """
|
||||
|
||||
|
|
@ -300,3 +301,28 @@ class XMLUI:
|
|||
def toXml(self):
|
||||
"""return the XML representation of the panel"""
|
||||
return self.doc.toxml()
|
||||
|
||||
|
||||
|
||||
class ElementParser(object):
|
||||
"""callable class to parse XML string into Element
|
||||
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, s):
|
||||
self.result = None
|
||||
def onStart(el):
|
||||
self.result = el
|
||||
def onEnd():
|
||||
pass
|
||||
def onElement(el):
|
||||
self.result.addChild(el)
|
||||
|
||||
parser = domish.elementStream()
|
||||
parser.DocumentStartEvent = onStart
|
||||
parser.ElementEvent = onElement
|
||||
parser.DocumentEndEvent = onEnd
|
||||
tmp = domish.Element(("", "s"))
|
||||
tmp.addRawXml(s.replace('\n','').replace('\t',''))
|
||||
parser.parse(tmp.toXml())
|
||||
return self.result.firstChildElement()
|
||||
|
|
|
|||
Loading…
Reference in a new issue