Aggiungo lo script che uso cosi magari puo' essere utile a qualcuno:
Codice PHP:
class Xml {
var $struct, $i, $xml_parser;
//Costruttore della classe
function Xml()
{
$this->xml_parser = xml_parser_create();
// Si utilizza il case-folding
xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, TRUE);
xml_set_object($this->xml_parser, $this);
xml_set_element_handler($this->xml_parser, 'start_element', 'end_element');
xml_set_character_data_handler($this->xml_parser, 'character_data');
$this->struct = array();
}
//Metodi pubblici
//Metodo per caricare file xml
function load($file,$compresses = FALSE)
{
$fopen = $compresses === TRUE ? 'gzopen' : 'fopen';
$fread = $compresses === TRUE ? 'gzread' : 'fread';
$feof = $compresses === TRUE ? 'gzeof' : 'feof';
if (!($fp = $fopen($file, 'r'))):
die("Non si riesce ad aprire il documento XML");
endif;
while ($data = $fread($fp, 4096)) {
if (!xml_parse($this->xml_parser, $data, $feof($fp))):
die(sprintf("Errore XML: %s alla linea %d",
xml_error_string(xml_get_error_code($this->xml_parser)),
xml_get_current_line_number($this->xml_parser)));
endif;
}
xml_parser_free($this->xml_parser);
}
//Metodo richiamato ad inizio
function start_element($parser, $name, $attrs)
{
$tag = array('name' => $name, 'attrs' => $attrs);
array_push($this->struct,$tag);
}
//Metodo chiamato per i dati
function character_data($parser, $data)
{
$this->struct[count($this->struct)-1]['data'] = $data;
}
//Metodo richiamato alla fine
function end_element($parser, $name)
{
$this->struct[count($this->struct)-2]['child'][] = $this->struct[count($this->struct)-1];
array_pop($this->struct);
}
}
e poi richiamo con
Codice PHP:
include('_include/class/xml.cls.php');
$parser = new xml();
$parser->load('ProductData.xml.gz',true);
print '<pre>';
print_r($parser->struct) ; //Contiene tutto il documento xml
print '</pre>';