php - How to print the array that contains the tags and the data in this xml parser? -
<?php class simple_parser { var $parser; var $error_code; var $error_string; var $current_line; var $current_column; var $data = array(); var $datas = array(); 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->dat1 = 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['child']; } xml_parser_free($this->parser); } function tag_open($parser, $tag, $attribs) { $this->data['child'][$tag][] = array('data' => '', 'attribs' => $attribs, 'child' => array()); $this->datas[] =& $this->data; $this->data =& $this->data['child'][$tag][count($this->data['child'][$tag])-1]; echo(""); } function cdata($parser, $cdata) { $this->data['data'] .= $cdata; echo "$cdata"; } function tag_close($parser, $tag) { $this->data =& $this->datas[count($this->datas)-1]; //echo "$this->datas[]"; array_pop($this->datas); } foreach ($this->data $i1 => $n1) foreach ($n1 $i2 => $n2) foreach ($n2 $i3 => $n3) printf('$data[%d][%d][%d] = %d;<br>', $i1,$i2,$i3,$n3);?> } $file = "blr_hosp-1.kml"; if (!($fp = fopen($file, "r"))) { die("could not open xml input"); } $data = fread($fp, filesize($file)); fclose($fp); $xml_parser = new simple_parser; $xml_parser->parse($data); ?>
the foreach loop didn't work . how give output of xml file parsed , have stored array . want print tags , data in tag .
looking @ code, there looks few syntactical errors. correct following errors first, , let know if problem persists:
- the line
printf('$data[%d][%d][%d] = %d;<br>', $i1,$i2,$i3,$n3);?>
has closing php tag @ end. (see?>
.) - i'm not sure 'foreach' loop correct you've placed it. reviewed braces (
{
,}
) few times make sure i'm right. looksforeach
function not contained inside function. looks it's still inside class though, worse..
my suggestion place 'foreach' statement, after removing ?> characters, after '$xml_parser->parse($data);
' command. (make sure edit $this
replaced $xml_parser
.)
i haven't reviewed rest of code errors--i may edit/update answer if i've suggested doesn't fix problem, or @ least figure out else wrong. :)
Comments
Post a Comment