code: def dom2object(node):
      
  
      value = []
      textnode_encountered = None
      for n in node.childNodes:
  
          if n.nodeType == 3:
              # Child is a text element - omit the dom tag #text and
              # go straight to the text value.
  
              # Note - only the last text value will be recorded
  
              msg = 'Text element has child nodes - this shouldn\'t happen'
              verify(len(n.childNodes) == 0, msg)
  
  
              x = n.nodeValue.strip()
              if len(x) == 0:
                  # Skip empty text children
                  continue
  
              textnode_encountered = value = x
          else:
              # XML element
  
  
              if textnode_encountered is not None:
                  msg = 'A text node was followed by a non-text tag. This is not allowed.\n'
                  msg += 'Offending text node: "%s" ' %str(textnode_encountered)
                  msg += 'was followed by node named: "<%s>"' %str(n.nodeName)
                  raise Exception, msg
  
  
              value.append(dom2object(n))
  
  
      # Deal with empty elements
      if len(value) == 0: value = ''
  
  
      if node.nodeType == 9:
          # Root node (document)
          tag = None
      else:
          # Normal XML node
          tag = node.nodeName
  
  
      X = XML_element(tag=tag,
                      value=value)
  
      return X
docs: Convert DOM representation to XML_object hierarchy.