XML parsing problems..

From: Date: Tue, 13 Feb 2001 21:45:55 +0000
Subject: XML parsing problems..
Groups: php.general 
Request: Send a blank email to php-general+get-39507@lists.php.net to get a copy of this message
I didn't see any responses to this the other day when I asked so this time I'll include source.. just cutting and pasting so hopefully nothing gets clobbered.. When I parse this lil bit of raw data it parses properly. You can trace it and see that it opens and closes the tags properly and that it writes the $this->dom variable properly in the parsers close_tag () function. However when you return the $this->dom value afterwards the variables previous value is returned rather than the current value. Any ideas what I am doing wrong? Thanks. <? //index file include_once ( 'common.inc' ); // does all my settings and includes.. in this case includes the below classes $raw = "<HTML><HEAD><TITLE>Howdy!</TITLE></HEAD>"; $raw .= "<BODY BGCOLOR='RED'>My dog is cute. My cat is cute.</BODY></HTML>"; $xml_parser = new xml_parser; $document = $xml_parser->parse ( $raw ); print "<PRE>"; print_r ( $document ); print "</PRE>"; ?> <? // classes file.. class xml_parser {
      var $encoding;
      var $parser;
      function xml_parser ( $encoding = 'ISO-8859-1' )
      {
              $this->encoding = $encoding;
              if ( $this->parser = xml_parser_create () ) {
                      xml_set_object ( $this->parser, $this );
                      xml_set_element_handler ( $this->parser, "tag_open", "tag_close" );
                      xml_set_character_data_handler ( $this->parser, "cdata");
              }
      }
      function parse ( $data )
      {
              xml_parse ( $this->parser, $data );
      }
      function tag_open ( $parser, $tag, $attributes )
      {
              foreach ( $attributes as $key => $value ) {
                      $attrib_str .= ", $key=$value";
              }
              print "<UL>\n";
              print "<LI>Open: $tag$attrib_str</LI>\n";
      }
      function cdata ( $parser, $cdata )
      {
              print "<LI>Data: $cdata</LI>\n";
      }
      function tag_close ( $parser, $tag )
      {
              print "<LI>Close: $tag</LI>\n";
              print "</UL>\n";
      }
} class xml_parser {
      var $encoding;
      var $parser;
      var $opened_tags = array ();
      var $dom;
      function xml_parser ( $encoding = 'ISO-8859-1' )
      {
              $this->encoding = $encoding;
              $this->create ();
              $this->configure ();
      }
      function create ()
      {
              $this->parser = xml_parser_create ( $this->encoding );
      }
      function configure ()
      {
              xml_set_object ( $this->parser, $this );
              xml_set_element_handler ( $this->parser, "open_tag", "close_tag" );
              xml_set_character_data_handler ( $this->parser, "cdata" );
      }
      function parse ( $data )
      {
              xml_parse ( $this->parser, $data );
              return $this->dom;
      }
      function open_tag ( $parser, $tag_type, $attributes )
      {
              $tag = new xml_tag ( $tag_type, $attributes );
              $this->opened_tags[] = $tag;
      }
      function close_tag ( $parser, $tag_type )
      {
              $tag = array_pop ( $this->opened_tags );
              $mother_tag = array_pop ( $this->opened_tags );
              if ( $mother_tag ) {
                      $mother_tag->add_tag ( $tag );
                      $this->opened_tags[] = $mother_tag;
              } elseif ( $tag ) {
                      $this->dom = $tag;
      function cdata ( $parser, $cdata )
      {
              $this->open_tag ( $parser, 'cdata', $cdata );
              $this->close_tag ( $parser, 'cdata' );
      }
} class xml_tag {
      var $tag;
      var $arguments;
      var $contents = array ();
      function xml_tag ( $tag, $arguments = array() )
      {
              // Name of the tag.
              $this->tag = $tag;
              // Array of argument/value pairs.
              $this->arguments = $arguments;
      }
      function add_tag ( $tag )
      {
              $this->contents[] = $tag;
      }
} ?>

« previous php.general (#39507) next »