5 doc = xml.dom.minidom.Document()
6 return self.__todom__(doc).toxml()
9 def __todom__(self, doc):
10 return doc.createTextNode(self)
13 def __init__(self, ns, name, ctx):
20 def __call__(self, *children, **attrs):
21 for child in children:
22 self.children.append(self.ctx.nodefrom(child))
23 for k, v in attrs.items():
24 self.attrs[str(k)] = str(v)
27 def __todom__(self, doc):
28 el = doc.createElementNS(self.ns, self.name)
29 for k, v in self.attrs.items():
31 for child in self.children:
32 el.appendChild(child.__todom__(doc))
35 class context(object):
38 self.nodeconv[bytes] = lambda ob: text(ob, "utf-8")
39 self.nodeconv[str] = text
40 self.nodeconv[int] = text
41 self.nodeconv[float] = text
43 def nodefrom(self, ob):
44 if isinstance(ob, node):
46 if hasattr(ob, "__tonode__"):
47 return ob.__tonode__()
48 if type(ob) in self.nodeconv:
49 return self.nodeconv[type(ob)](ob)
50 raise Exception("No node conversion known for %s objects" % str(type(ob)))
52 class constructor(object):
53 def __init__(self, ns, elcls = element, ctx=None):
56 if ctx is None: ctx = context()
59 def __getattr__(self, name):
60 return self._elcls(self._ns, name, self._ctx)