 -        $this->_cnt++;
 -        $this->_objs[$this->_cnt]= $element;
 -      }
-+      array_unshift($this->stack, new $this->nodeType($name, NULL, $attrs));
-     }
-    
+-    }
+-   
      /**
-      * Callback function for XMLParser
+-     * Callback function for XMLParser
++     * Creates a string representation of this object
       *
-+     * @deprecated Use xml.parser.TreeParser instead
-      * @param   resource parser
-      * @param   string name
-      * @see     xp://xml.parser.XMLParser
-      */
-     public function onEndElement($parser, $name) {
+-     * @param   resource parser
+-     * @param   string name
+-     * @see     xp://xml.parser.XMLParser
++     * @return  string
+      */
+-    public function onEndElement($parser, $name) {
 -      if ($this->_cnt > 1) {
 -        $node= $this->_objs[$this->_cnt];
 -        $node->content= $this->_cdata;
 -        $parent= $this->_objs[$this->_cnt- 1];
 -        $parent->addChild($node);
 -        $this->_cdata= '';
-+      $c= array_shift($this->stack);
-+      if ($this->stack) {
-+        $this->stack[0]->addChild($c);
-       } else {
+-      } else {
 -        $this->root->content= $this->_cdata;
 -        $this->_cdata= '';
-+        $this->root= $c;
-       }
+-      }
 -      $this->_cnt--;
 -      
 -      // Clean up upon last element
 -      if (0 === $this->_cnt) {
 -        unset($this->_cnt, $this->_cdata, $this->_objs);
 -      }
-     }
- 
-     /**
-      * Callback function for XMLParser
-      *
-+     * @deprecated Use xml.parser.TreeParser instead
-      * @param   resource parser
-      * @param   string cdata
-      * @see     xp://xml.parser.XMLParser
-      */
-     public function onCData($parser, $cdata) {
--      $this->_cdata.= $cdata;
-+    
-+      // Ignore tokens consisting entirely of whitespace
-+      if (strlen($cdata) !== strspn($cdata, " \r\n\t")) {
-+        $this->stack[0]->addChild(new Text($cdata));
-+      }
-     }
- 
-     /**
-      * Callback function for XMLParser
-      *
-+     * @deprecated Use xml.parser.TreeParser instead
-      * @param   resource parser
-      * @param   string data
-      * @see     xp://xml.parser.XMLParser
-      */
-     public function onDefault($parser, $data) {
-     }
-+
-+    /**
-+     * Creates a string representation of this object
-+     *
-+     * @return  string
-+     */
 +    public function toString() {
 +      return sprintf(
 +        "%s(version= %s encoding=%s) {\n  %s\n}",

       return $this->root->addChild($child);
     }
 
@@ -112,6 +110,7 @@
      *   $tree= Tree::fromString('<document>...</document>');
      * </code>
      *

+     */
+    public function onEndElement($parser, $name) {
+      $c= array_shift($this->stack);
+      if ($this->stack[0]) {
+        $this->stack[0]->addChild($c);
+      } else {
+        $this->stack[0]= $c;
+      }
+    }
+
