# a quick and dirty xml module for parsing and generating xml/html # # this is a very poor man's xml parser # it uses the python syntax parser for parsing xml code # and defines a tag class called T. xml code is first translated to # valid python code and then evaluated. works also under jython. # # (c) f.jamitzky 2006 class T: def __init__(self,name,args=[]): arglist=name.split(" ") self._name=arglist[0] self._kw={} self._args=args if len(arglist)>1: kw={} for i in arglist[1:]: key, val= i.split("=") kw[key]=val self._kw=kw def __len__(self): return len(self._args) def __str__(self): if self._args==[]: if self._kw=={}: txt="<"+self._name+"/>" else: txt="<"+self._name for i in self._kw.keys(): txt+=" "+str(i)+"="+str(self._kw[i])+" " txt=txt[:-1]+"/>" else: if self._kw=={}: txt="<"+self._name+">" else: txt="<"+self._name for i in self._kw.keys(): txt+=" "+str(i)+"="+str(self._kw[i])+" " txt=txt[:-1]+">" for arg in self._args: txt+=str(arg) txt+="" return txt def __repr__(self): return str(self) def __getitem__(self,key): if type(key)==type(0): return self._args[key] elif type(key)==type(""): return self._kw[key] def __setitem__(self,key,value): if type(key)==type(0): if key").replace("]","") data=data.replace("\n","").replace('"',"'") data=data.replace("?>","?/>").replace("-->","--/>") data=data.replace("","[]mptytag[") data=data.replace("<","[]starttag[").replace(">","[]closetag[") data=data.split("[") outstr='' i=-1 lendata=len(data) while i/ab""" print "xml string:" print data tt=xml(data) print "print:" print tt print "print tags:" print tt.tags() print "get tag 'a':" print tt.a print "generating html:" html=xml("") html.body.append("Hello World from jython") html.head['title']="Hello World" print html print ""