import xml.etree.ElementTree as ET import sys,re from CompactTokenizer import CompactTokenizer def expand(elem): global ct ct.skip("[") while ct.getToken()=='@': attName = ct.nextToken() ct.nextToken() elem.attrib[attName]=ct.skip("[") ct.nextToken() ct.skip("]") lastNodeWasText=True while ct.getToken()!=']': s=ct.getToken().strip() ct.nextToken() if ct.getToken()=="[": child=ET.Element(s) elem.append(child) expand(child) lastNodeWasText=False else: if lastNodeWasText: if not elem.text: elem.text="" elem.text+=s else: if not child.tail: child.tail="" child.tail+=s ct.skip("]") ct = CompactTokenizer(sys.stdin) while ct.nextToken()!='[': rootName=ct.getToken() doc=ET.Element(rootName.strip()) expand(doc) # ElementTree does not provide a pretty-print method so we define one def pprint(elem,indent): sys.stdout.write(indent+'<'+elem.tag) for a in elem.attrib: sys.stdout.write(' %s="%s"'%(a,elem.attrib[a])) newindent = indent+" " sys.stdout.write('>') if elem.text: sys.stdout.write(elem.text) for child in elem: pprint(child,newindent) if child.tail: sys.stdout.write(newindent+child.tail) if elem: sys.stdout.write(indent) sys.stdout.write('') pprint(doc,"\n") sys.stdout.write("\n")