note 58778 added to ref.xml
| From: | adrian_popescu at yahoo dot com | Date: | Tue, 15 Nov 2005 19:04:59 +0000 |
| Subject: | note 58778 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98623@lists.php.net to get a copy of this message | ||
Raphael's XMLParser has a small bug in the dataHandler function: it cannot handle multiple line
values correctly. For instance:
<mytag> This is a
very long
line
</mytag>
causes the parser to return this content:
Array
(
[name] => MYTAG
[content] => line
)
whereas one would have expected the
[content] => 'This is a very long line'.
Below is the fixed version:
<?php
class XMLParser {
var $filename;
var $xml;
var $data;
function XMLParser($xml_file)
{
$this->filename = $xml_file;
$this->xml = xml_parser_create();
xml_set_object($this->xml, $this);
xml_set_element_handler($this->xml, 'startHandler', 'endHandler');
xml_set_character_data_handler($this->xml, 'dataHandler');
$this->parse($xml_file);
}
function parse($xml_file)
{
if (!($fp = fopen($xml_file, 'r'))) {
die('Cannot open XML data file: '.$xml_file);
return false;
}
$bytes_to_parse = 512;
while ($data = fread($fp, $bytes_to_parse)) {
$parse = xml_parse($this->xml, $data, feof($fp));
if (!$parse) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml)),
xml_get_current_line_number($this->xml)));
xml_parser_free($this->xml
);
}
}
return true;
}
function startHandler($parser, $name, $attributes)
{
$data['name'] = $name;
if ($attributes) { $data['attributes'] = $attributes; }
$this->data[] = $data;
}
function dataHandler($parser, $data)
{
if ($data = trim($data)) {
$index = count($this->data) - 1;
// begin multi-line bug fix (use the .= operator)
$this->data[$index]['content'] .= $data;
// end multi-line bug fix
}
}
function endHandler($parser, $name)
{
if (count($this->data) > 1) {
$data = array_pop($this->data);
$index = count($this->data) - 1;
$this->data[$index]['child'][] = $data;
}
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58778
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58778&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58778&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58778&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58778&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58778&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58778&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58778&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58778&report=yes
Search -- http://master.php.net/manage/user-notes.php