note 71837 added to ref.xml
| From: | vavricek at volny dot cz | Date: | Mon, 18 Dec 2006 19:53:50 +0000 |
| Subject: | note 71837 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120756@lists.php.net to get a copy of this message | ||
RE: forquan (29-Jan-2006 12:45)
Thanks, for your code (it was what I need), but ... it didn't works with my XML file. I think
that you tested it on simple XML. Never mind.
I change few lines (problem was in endHandler function), and now it WORKS :-)
<?php
$p =& new xmlParser();
$p->parse("/* XML file*/");
echo "<pre>";
print_r($p->output);
echo "</pre>";
class xmlParser{
var $xml_obj = null;
var $output = array();
var $attrs;
function xmlParser(){
$this->xml_obj = xml_parser_create();
xml_set_object($this->xml_obj,$this);
xml_set_character_data_handler($this->xml_obj, 'dataHandler');
xml_set_element_handler($this->xml_obj, "startHandler", "endHandler");
}
function parse($path){
if (!($fp = fopen($path, "r"))) {
die("Cannot open XML data file: $path");
return false;
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($this->xml_obj, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->xml_obj)),
xml_get_current_line_number($this->xml_obj)));
xml_parser_free($this->xml_obj);
}
}
return true;
}
function startHandler($parser, $name, $attribs){
$_content = array();
$_content['name'] = $name;
if(!empty($attribs))
$_content['attrs'] = $attribs;
array_push($this->output, $_content);
}
function dataHandler($parser, $data){
if(!empty($data) && $data!="\n") {
$_output_idx = count($this->output) - 1;
$this->output[$_output_idx]['content'] .= $data;
}
}
function endHandler($parser, $name){
if(count($this->output) > 1) {
$_data = array_pop($this->output);
$_output_idx = count($this->output) - 1;
$add = array();
if(!$this->output[$_output_idx]['child'])
$this->output[$_output_idx]['child'] = array();
array_push($this->output[$_output_idx]['child'], $_data);
}
}
}
?>
----
Server IP: 147.32.160.17
Probable Submitter: 81.30.255.170 (proxied: 10.153.198.57)
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- https://master.php.net/note/edit/71837
Del: integrated -- https://master.php.net/note/delete/71837/integrated
Del: useless -- https://master.php.net/note/delete/71837/useless
Del: bad code -- https://master.php.net/note/delete/71837/bad+code
Del: spam -- https://master.php.net/note/delete/71837/spam
Del: non-english -- https://master.php.net/note/delete/71837/non-english
Del: in docs -- https://master.php.net/note/delete/71837/in+docs
Del: other reasons-- https://master.php.net/note/delete/71837
Reject -- https://master.php.net/note/reject/71837
Search -- https://master.php.net/manage/user-notes.php