note 79397 deleted from ref.xml by danbrown
| From: | danbrown@php.net | Date: | Wed, 03 Mar 2010 18:56:00 +0000 |
| Subject: | note 79397 deleted from ref.xml by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166624@lists.php.net to get a copy of this message | ||
Note Submitter: yousuf at philipz dot com
----
Here is my modification of < dmeekins att gmail doot com > XMLParser class, as i have used it
for quite a bit. There were 2 problems with his post, which of course was a modification of an
earlier post, so the problem continued through the many versions. The problems were in the
dataHandler function. The first problem was with '$data = trim($data);' which removed line
breakers from data which went over many lines and the second problem was when a tag had a value 0.
So here is the corrected function.
<?php
function dataHandler($parser, $data)
{
if(!empty($data) || strval($data) != "" )
{
if(isset($this->currTag['data']))
$this->currTag['data'] .= $data;
else
$this->currTag['data'] = $data;
}
}
?>
By removing '$data = trim($data);', you will notice that some [data] elements, mainly the
root ones, will have alot of line breakers in them with no actual data.
The code by < geoffers [at] gmail [dot] com > was also quite good as it keeps things alot
smaller than XMLParser and here's my modification of part of his code, as i preferred to have
it look similar to how XMLParser has it (removes the ['child'] entry and changes
'attribs' to 'attr').
<?php
function parse($data)
{
$this->parser = xml_parser_create('UTF-8');
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
xml_set_element_handler($this->parser, 'tag_open', 'tag_close');
xml_set_character_data_handler($this->parser, 'cdata');
if (!xml_parse($this->parser, $data))
{
$this->data = array();
$this->error_code = xml_get_error_code($this->parser);
$this->error_string = xml_error_string($this->error_code);
$this->current_line = xml_get_current_line_number($this->parser);
$this->current_column = xml_get_current_column_number($this->parser);
}
else
{
$this->data = $this->data;
}
xml_parser_free($this->parser);
}
function tag_open($parser, $tag, $attribs)
{
$this->data[$tag][] = array('data' => '', 'attr' =>
$attribs);
$this->datas[] =& $this->data;
$this->data =& $this->data[$tag][count($this->data[$tag])-1];
}
?>
The code by < adamaflynn at criticaldevelopment dot net > and < geoff at spacevs dot com
> are also quite good but use xmlObject object rather than standard arrays.