1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/XMLTable.py Sun Jul 24 22:18:56 2005 +0000
1.3 @@ -0,0 +1,92 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"A list of tuples to XML document converter."
1.7 +
1.8 +import libxml2dom
1.9 +from UserDict import UserDict
1.10 +
1.11 +class OrderedDict(UserDict):
1.12 +
1.13 + "A dictionary with keys in the order in which they were given."
1.14 +
1.15 + def __init__(self, *args):
1.16 + UserDict.__init__(self, *args)
1.17 + self._keys = self.data.keys()
1.18 +
1.19 + def __setitem__(self, key, value):
1.20 + UserDict.__setitem__(self, key, value)
1.21 + if key not in self._keys:
1.22 + self._keys.append(key)
1.23 +
1.24 + def __delitem__(self, key):
1.25 + UserDict.__delitem__(self, key)
1.26 + del self._keys[key]
1.27 +
1.28 + def keys(self):
1.29 + return self._keys
1.30 +
1.31 + def items(self):
1.32 + l = []
1.33 + for key in self._keys:
1.34 + l.append((key, self.data[key]))
1.35 + return l
1.36 +
1.37 + def values(self):
1.38 + return [value for key, value in self.items()]
1.39 +
1.40 +class Converter:
1.41 + def __init__(self, ns=None, prefix="", doc=None, root=None):
1.42 + self.ns = ns
1.43 + self.prefix = prefix
1.44 + if doc is not None:
1.45 + self.doc = doc
1.46 + else:
1.47 + self.doc = libxml2dom.createDocument(ns, prefix+"table", None)
1.48 + if root is not None:
1.49 + self.root = root
1.50 + else:
1.51 + self.root = self.doc.xpath("*")[0]
1.52 +
1.53 + def add_rows(self, rows):
1.54 + for row in rows:
1.55 + self.add_row(row)
1.56 +
1.57 + def add_row(self, row, index=-1, row_element_name="row", use_key_as_element_name=0):
1.58 + row_element = self.doc.createElementNS(self.ns, self.prefix+row_element_name)
1.59 + if index == -1:
1.60 + self.root.appendChild(row_element)
1.61 + else:
1.62 + self.root.insertBefore(row_element, self.root.xpath("*[%s]" % (index + 1))[0])
1.63 +
1.64 + # Permit dictionaries.
1.65 +
1.66 + if hasattr(row, "items"):
1.67 + for name, value in row.items():
1.68 + if use_key_as_element_name:
1.69 + column_element = self.doc.createElementNS(self.ns, self.prefix+unicode(name))
1.70 + else:
1.71 + column_element = self.doc.createElementNS(self.ns, self.prefix+"column")
1.72 + column_element.setAttributeNS(self.ns, self.prefix+"name", unicode(name))
1.73 + row_element.appendChild(column_element)
1.74 + text = self.doc.createTextNode(unicode(value))
1.75 + column_element.appendChild(text)
1.76 +
1.77 + # As well as sequences.
1.78 +
1.79 + else:
1.80 + for column in row:
1.81 + column_element = self.doc.createElementNS(self.ns, self.prefix+"column")
1.82 + row_element.appendChild(column_element)
1.83 + text = self.doc.createTextNode(unicode(column))
1.84 + column_element.appendChild(text)
1.85 +
1.86 +if __name__ == "__main__":
1.87 + from rdflib.TripleStore import TripleStore
1.88 + import sys
1.89 + s = TripleStore()
1.90 + s.load(sys.argv[1])
1.91 + converter = Converter()
1.92 + converter.add_rows(s.triples((None, None, None)))
1.93 + libxml2dom.toStream(converter.doc, sys.stdout)
1.94 +
1.95 +# vim: tabstop=4 expandtab shiftwidth=4