Re: FN-FORUM: php xml_parser
date posted 15th April 2008 14:17
On Tue, 2008-04-15 at 14:34 +0000, Steven wrote:
> class xml {
> var $parser;
> var $pointer;
> var $dom;
> function xml() {
> $this->pointer =& $this->dom;
> $this->parser = xml_parser_create();
> xml_set_object($this->parser, $this);
> xml_parser_set_option($this->parser,
> XML_OPTION_CASE_FOLDING, false);
> 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 makeChildNode() {
> if (!is_array($this->pointer['child_nodes'])){
> $this->pointer['child_nodes'] = array();
> }
> return count($this->pointer['child_nodes']);
> }
>
> function tag_open($parser, $tag, $attributes) {
> $idx = $this->makeChildNode();
> $this->pointer['child_nodes'][$idx] = Array(
> '_idx' => $idx,
> '_parent' => &$this->pointer,
> 'tag_name' => $tag,
> 'attributes' => $attributes,
> );
> $this->pointer =& $this->pointer['child_nodes'][$idx];
> }
>
> function cdata($parser, $cdata) {
> //drop text nodes that are just white space formatting
> characters
> if (trim($cdata) != "") {
> $idx = $this->makeChildNode();
> $this->pointer['child_nodes'][$idx] = $cdata;
> }
> }
>
> function tag_close($parser, $tag) {
> $idx =& $this->pointer['_idx'];
> $this->pointer =& $this->pointer['_parent'];
> unset($this->pointer['child_nodes'][$idx]['_idx']);
> unset($this->pointer['child_nodes'][$idx]['_parent']);
> }
> }
>
Sorry: I didn't see this chunk at the bottom of you mail. Did you
consider using one of PHP's built-in xml parsers? The expat one is very
fast and doesn't use many resources at at.
Graham
--
Graham Stark, Virtual Worlds, http://www.virtual-worlds.biz
Phone (+44) 01908 618239 Mobile (+44) 07952633185 Skype graham_k_stark