require 'rexml/document' include REXML require 'compactTokenizer' st = CompactTokenizer.new(STDIN) # start new document doc = Document.new() doc << XMLDecl.new(1.0,"UTF-8") # find the name of the root rootName = 'dummyElement' while st.nextToken!='[' rootName=st.token end # create the element and return it with the current token def expand(st,elementName) elem = Element.new(elementName) st.skip("[") while st.token=='@' # process attributes attName = st.skip("@") st.nextToken elem.attributes[attName] = st.skip("[") st.nextToken st.skip("]") end while st.token!=']' # process children s = st.token.strip st.nextToken if(st.token=='[') elem << expand(st,s) else elem << Text.new(s) end end st.skip("]") return elem end # expand from the root element doc << expand(st,rootName) # write it properly indented doc.write(STDOUT,0)