note 25301 added to ref.xml

From: Date: Wed, 18 Sep 2002 17:27:19 +0000
Subject: note 25301 added to ref.xml
Groups: php.notes 
Request: Send a blank email to php-notes+get-36898@lists.php.net to get a copy of this message
Some reference code I am working on as "XML Library" of which I am folding it info an object. Notice the use of the DEFINE: Mainly Example 1 and parts of 2 & 3 re-written as an object: --- MyXMLWalk.lib.php --- <?php if (!defined("PHPXMLWalk")) { define("PHPXMLWalk",TRUE); class XMLWalk { var $p; //short for xml parser; var $e; //short for element stack/array function prl($x,$i=0) { ob_start(); print_r($x); $buf=ob_get_contents(); ob_end_clean(); return join("\n".str_repeat(" ",$i),split("\n",$buf)); } function XMLWalk() { $this->p = xml_parser_create(); $this->e = array(); xml_parser_set_option($this->p, XML_OPTION_CASE_FOLDING, true); xml_set_element_handler($this->p, array(&$this, "startElement"), array(&$this, "endElement")); xml_set_character_data_handler($this->p, array(&$this, "dataElement")); register_shutdown_function(array(&$this, "free")); // make a destructor } function startElement($parser, $name, $attrs) { if (count($attrs)>=1) { $x = $this->prl($attrs, $this->e[$parser]+6); } else { $x = ""; } print str_repeat(" ",$this->e[$parser]+0). "$name $x\n"; $this->e[$parser]++; $this->e[$parser]++; } function dataElement($parser, $data) { print str_repeat(" ",$this->e[$parser]+0). htmlspecialchars($data, ENT_QUOTES) ."\n"; } function endElement($parser, $name) { $this->e[$parser]--; $this->e[$parser]--; } function parse($data, $fp) { if (!xml_parse($this->p, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->p)), xml_get_current_line_number($this->p))); } } function free() { xml_parser_free($this->p); } } // end of class } // end of define ?> --- end of file --- Calling code: <?php ... require("MyXMLWalk.lib.php"); $file = "x.xml"; $xme = new XMLWalk; if (!($fp = fopen($file, "r"))) { die("could not open XML input"); } while ($data = fread($fp, 4096)) { $xme->parse($data, $fp); } ... ?> -- http://www.php.net/manual/en/ref.xml.php http://master.php.net/manage/user-notes.php?action=edit+25301 http://master.php.net/manage/user-notes.php?action=delete+25301 http://master.php.net/manage/user-notes.php?action=reject+25301

« previous php.notes (#36898) next »