Skip to content

Instantly share code, notes, and snippets.


Created Jan 24, 2014
What would you like to do?
from functools import wraps
from xml.etree import cElementTree as etree
def coroutine(func):
def start(*args, **kwargs):
cr = func(*args, **kwargs)
return cr
return start
def builder(root):
def process(parent, current, ctag, event, v1, v2):
if event == 'start':
tag = v1
if tag[0] == '{':
tag = tag.split('}', 1)[1]
newnode = {}
child = current[ctag]
except KeyError:
child = current[ctag] = []
return (parent, current, ctag), newnode, tag
elif event == 'data':
print ctag, v1
current[ctag] = v1
return parent, current, ctag
elif event == 'end':
return parent
parent, node, tag = (None, None, None), root, 'root'
while node:
event, v1, v2 = (yield)
parent, node, tag = process(parent, node, tag, event, v1, v2)
except Exception as e:
import traceback
class DictBuilder:
def __init__(self):
self.result = {}
self.builder = builder(self.result)
def start(self, tag, attrib):
self.builder.send(('start', tag, attrib))
def data(self, data):
self.builder.send(('data', data, None))
def end(self, tag):
self.builder.send(('end', tag, None))
def close(self):
return self.result
def parsestring(xml):
parser = etree.XMLParser(target=DictBuilder())
return parser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment