Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > note 58570 deleted from ref.xml by bjori
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 21st December 08:25
bjori
External User
 
Posts: 1
Default note 58570 deleted from ref.xml by bjori


Note Submitter: raphael at schwarzschmid dot de

----

Monte's class with its various amendments didn't quite work for me, so here's my version of it:

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;
$this->data[$index]['content'] = $data;
}
}

function endHandler($parser, $name)
{
if (count($this->data) > 1) {
$data = array_pop($this->data);
$index = count($this->data) - 1;
$this->data[$index]['child'][] = $data;
}
}
}

Use like:

$myFile = new XMLParser($path_to_file);
echo $myFile->data[$n]['name'];
foreach ($myFile->data[$n]['attributes'] as $key => $val)
echo $key.'='.$val;

.... and so forth.

If somebody would know how to say something like...

$myFile = new XMLParser($path_to_file);
echo $myFile[$n];
foreach ($myFile[$n]['attributes'] as $key => $val)
echo $key.'='.$val;

....instead, I'd be very interested in that!
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666