from xml.dom.minidom import getDOMImplementation class rdf: child = [] items = None name = None def __init__(self, name, items = None ): self.name = name self.items = items self.child = [] def append(self, rdf): self.child.append(rdf) def toXML(self, xmlns): impl = getDOMImplementation() doc = impl.createDocument("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "RDF:RDF", None) root = doc.documentElement root.setAttribute("xmlns:RDF",'http://www.w3.org/1999/02/22-rdf-syntax-ns#') root.setAttribute("xmlns:LAP",xmlns+"/rdf#") Seq = self.genSeq(doc, root, xmlns) root.appendChild(Seq) return '\n'+root.toxml("UTF-8") def genSeq(self, doc, root, about): Seq = doc.createElement('RDF:Seq') Seq.setAttribute("RDF:about",about+"/"+self.name) # if self.items != None: Description = doc.createElement('RDF:Description') Description.setAttribute("RDF:about",about+"/"+self.name) root.appendChild(Description) for a in self.items: #print a + " : " + self.items[a] elm = doc.createElement('LAP:'+a) txt = doc.createTextNode(str(self.items[a])) elm.appendChild(txt) Description.appendChild(elm) if len(self.child) != 0: for a in self.child: #a = self.child[0]; li = doc.createElement('RDF:li') Seq.appendChild(li) li.appendChild( a.genSeq(doc, root, about+"/"+self.name) ) #print self.child, len(self.child) return Seq if __name__ == "__main__": x = rdf( "all-test" ) x.append( rdf("maisons", {"id":"1", "addr":"toto"} ) ) x.append( rdf("homes", {"id":"2", "addr":"titi"} ) ) x.append( rdf("domoj", {"id":"3", "addr":"tutu"} ) ) y = rdf("tutu", {"id":"5", "addr":"fsdjkhf fhsqdljk fhsjdk hl"} ) y.append( rdf("fds", {"id":"4", "addr":"fsdfds"} ) ) x.append( y ) print x.toXML("http://www.lapinator.net") #f = open('test.xml','w') #d = x.toXML("http://www.lapinator.net") #f.write(d) #f.close()