 parseEmptyElement = (s) ->
   if s.match(/^<\/\S+$/)
-    [s.substring(2), 'empty element']
+    name = s.substring(2)
+    [name, 'empty element', null, ->
+      attributes = (" #{k}=\"#{v}\"" for k, v of @attributes).join ''
+      '<' + name + attributes + ' />'
+    ]
 
 parseElement = (s) ->
   if s.match(/^<\S+$/)
-    [s.substring(1), 'element']
+    name = s.substring(1)
+    [name, 'element', null, ->
+      attributes = (" #{k}=\"#{v}\"" for k, v of @attributes).join ''
+      children = @children.map((i) -> i.write()).join('')
+      '<' + name + attributes + '>' + children + '</' + name + '>'
+    ]
 
 parseAttribute = (s) ->
   m = s.match(/^@(\S+)\s+(.+)$/)
   if m
-    [m[1], 'attribute', m[2]]
+    [m[1], 'attribute', m[2], null]
 
 parseText = (s) ->
   if s.match(/^>.+$/)
-    [s.substring(1), 'text']
+    name = s.substring(1)
+    [name, 'text', null, -> name]
 
 class Node
-  constructor: ({ @level, @name, @type, @value }) ->
+  constructor: ({ @level, @parsed, @name, @type, @value }) ->
     @parent = null
     @attributes = {}
     @children = []

      parseAttribute
      parseText
      (s) ->
        [s, 'text']
    ].some (f) ->
      parsed = f node
    new Node { level, name: parsed[0], type: parsed[1], value: parsed[2] }

  setAttribute: (name, value) ->
    @attributes[name] = value

  appendChild: (n) ->
    @children.push n
    n.parent = @

  write: ->
    @parsed[3].apply @, []

parse = (s) ->
  root = Node.parse '<root'
