ecco cosa ho creato alla fine
codice:
<?php
$struct = array();
function startElement($parser, $name, $attrs){
global $struct, $current_tag;
$tag = array($name=>'', 'attrs'=> $attrs);
array_push($struct,$tag);
$current_tag = $name;
}
function data($parser, $data){
global $struct, $current_tag;
if(trim($data)){
$struct[count($struct)-1][$current_tag]=$data;
}
}
function endElement($parser, $name){
global $struct, $current_tag;
$struct[count($struct)-2][] = $struct[count($struct)-1];
array_pop($struct);
if ($name == "DATI"){
for ($i = 0; $i <= count($struct[0][0][0]); $i++){
if (!empty($struct[0][0][0][$i]['VERSION']))
$version = $struct[0][0][0][$i]['VERSION'];
if (!empty($struct[0][0][0][$i]['BODY']))
$body = $struct[0][0][0][$i]['BODY'];
}
$struct = array('');
}
}
function parse_file($file){
global $struct;
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "data");
$parse = xml_parse($xml_parser,file_get_contents($file));
if(!$parse) {
die("Errore parsing XML");
xml_parser_free($xml_parser);
}
return $struct;
}
$data = parse_file("file.xml");
?>
certo che lavorare su un file xml è proprio complicato
devo usare un FOR perchè i dati in un determinato campo possono aumentare ess.:
codice:
<?xml version="1.0" encoding="utf-8" ?>
<export>
<specifics>
<version></version>
<body></body>
</specifics>
<specifics>
<version></version>
<label></label>
<body></body>
</specifics>
</export>
in specifics i campi possono aumentare e quindi non li posso richiamare con lo stesso numero.
non riesco a ricavare una struttura ad albero come dico io
codice:
$struct[export][1][specifics][version]