note 79397 added to ref.xml
| From: | yousuf at philipz dot com | Date: | Sun, 25 Nov 2007 07:53:20 +0000 |
| Subject: | note 79397 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-132552@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 213.42.2.22 (proxied: 217.164.179.254)
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- https://master.php.net/note/edit/79397
Del: integrated -- https://master.php.net/note/delete/79397/integrated
Del: useless -- https://master.php.net/note/delete/79397/useless
Del: bad code -- https://master.php.net/note/delete/79397/bad+code
Del: spam -- https://master.php.net/note/delete/79397/spam
Del: non-english -- https://master.php.net/note/delete/79397/non-english
Del: in docs -- https://master.php.net/note/delete/79397/in+docs
Del: other reasons-- https://master.php.net/note/delete/79397
Reject -- https://master.php.net/note/reject/79397
Search -- https://master.php.net/manage/user-notes.php