   appendChild: (n) ->
     @children.push n
     n.parent = @
+
+  write: ->
+    @parsed[3].apply @, []
 
 parse = (s) ->
   root = Node.parse '<root'

parseEmptyElement = (s) ->
  if s.match(/^<\/\S+$/)
    [s.substring(2), 'empty element']

parseElement = (s) ->
  if s.match(/^<\S+$/)
    [s.substring(1), 'element']

parseAttribute = (s) ->
  m = s.match(/^@(\S+)\s+(.+)$/)
  if m
    [m[1], 'attribute', m[2]]

parseText = (s) ->
  if s.match(/^>.+$/)
    [s.substring(1), 'text']

class Node
  constructor: ({ @level, @name, @type, @value }) ->
    @parent = null
    @attributes = {}
    @children = []

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

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