5 doc = xml.dom.minidom.Document()
6 return self.__todom__(doc).toxml()
8 class text(node, unicode):
9 def __todom__(self, doc):
10 return doc.createTextNode(self)
13 def __init__(self, ns, name, ctx):
15 self.name = unicode(name)
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.iteritems():
24 self.attrs[unicode(k)] = unicode(v)
27 def __todom__(self, doc):
28 el = doc.createElementNS(self.ns, self.name)
29 for k, v in self.attrs.iteritems():
31 for child in self.children:
32 el.appendChild(child.__todom__(doc))
35 class context(object):
38 self.nodeconv[str] = lambda ob: text(ob, "utf-8")
39 self.nodeconv[unicode] = text
40 self.nodeconv[int] = text
41 self.nodeconv[long] = text
42 self.nodeconv[float] = text
44 def nodefrom(self, ob):
45 if isinstance(ob, node):
47 if hasattr(ob, "__tonode__"):
48 return ob.__tonode__()
49 if type(ob) in self.nodeconv:
50 return self.nodeconv[type(ob)](ob)
51 raise Exception("No node conversion known for %s objects" % str(type(ob)))
53 class constructor(object):
54 def __init__(self, ns, elcls = element, ctx=None):
57 if ctx is None: ctx = context()
60 def __getattr__(self, name):
61 return self._elcls(self._ns, name, self._ctx)