PDA

View Full Version : [PHP - XML] Estrarre attribiti da un tag XML


CheccoSR
02-09-2008, 15:36
Ciao ragazzi!

Ho un file XML di con questa struttura:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<datiposttrade>
<abi>05104</abi>
<datavis>2008-08-05</datavis>
<FUTLO pc_id="P0" version="0100" fine="S">
<riga_potr>
<prg_con>1</prg_con>
<dtese>2008-08-05</dtese>
<hhese>11:35:00</hhese>
<isin des="Fiat Az.Ord Ragrr">IT0001976403</isin>
<kdiv>EUR</kdiv>
<qta>200</qta>
<prz>10.47</prz>
<tcor>S</tcor>
<sede>OTC</sede>
<ind>C</ind>
</riga_potr>
<riga_potr>
<prg_con>2</prg_con>
<dtese>2008-08-05</dtese>
<hhese>11:45:00</hhese>
<isin des="Telecom Italia Ol">IT0003497168</isin>
<kdiv>EUR</kdiv>
<prz>10</prz>
<tqta>C</tqta>
<tcor>S</tcor>
<sede>OTC</sede>
</riga_potr>
</FUTLO>
</datiposttrade>


Sono riuscito a estrarre tutti i dati tranne quelli compresi nel tag <isin>

Come faccio ad estrarre l'attributo "des" dal tag <isin>?!


Grazie :)

Torav
02-09-2008, 18:19
ma ti sei fatto un parser tu (spero di no :stordita: ) o ne hai scaricato uno o usi le funzioni che ti fornisce php?

CheccoSR
03-09-2008, 08:36
ma ti sei fatto un parser tu (spero di no :stordita: ) o ne hai scaricato uno o usi le funzioni che ti fornisce php?

ho usato quelle messe a disposizione da php http://it2.php.net/manual/en/ref.xml.php

Torav
03-09-2008, 11:09
allora nella funzione che bindi quando il parser incontra un tag di apertura hai $attrs che è un array contenente come chiavi i nomi degli attributi (in questo caso des) e il valore associato è il contenuto dell'attributo (in questo caso Telecom Italia Ol)

CheccoSR
04-09-2008, 08:55
allora nella funzione che bindi quando il parser incontra un tag di apertura hai $attrs che è un array contenente come chiavi i nomi degli attributi (in questo caso des) e il valore associato è il contenuto dell'attributo (in questo caso Telecom Italia Ol)

innanzittutto grazie x l'interessamente!
ti scrivo il codice che sto usando...

// APRE E LEGGE L'XML

$filename = array();
$xmlC = array();
$xml_data = array();


$filename = "../xml/post_trade.xml";

$xmlC = new XmlC();
$xml_data = file_get_contents($filename);
$xmlC -> Set_XML_data($xml_data);

class XmlC {

var $xml_data;
var $obj_data;
var $pointer;


function XmlC() {}

function Set_xml_data(&$xml_data) {

$this->index = 0;
$this->pointer[] = &$this->obj_data;
$this->xml_data = $xml_data;
$this->xml_parser = xml_parser_create( "ISO-8859-1" );

xml_parser_set_option( $this->xml_parser, XML_OPTION_CASE_FOLDING, false );
xml_set_object( $this->xml_parser, $this );
xml_set_element_handler( $this->xml_parser, "_startElement", "_endElement");
xml_set_character_data_handler( $this->xml_parser, "_cData" );

xml_parse( $this->xml_parser, $this->xml_data, true );
xml_parser_free( $this->xml_parser );
}

function _startElement( $parser, $tag ) {

$object = "";

eval( "\$this->pointer[\$this->index]->" . $tag . "[] = \$object;" );

eval( "\$size = sizeof( \$this->pointer[\$this->index]->" . $tag . " );" );
eval( "\$this->pointer[] = &\$this->pointer[\$this->index]->" . $tag . "[\$size-1];" );

$this->index++;

}

function _endElement($parser, $tag) {
array_pop( $this->pointer );
$this->index--;
}

function _cData( $parser, $data ) {


if(trim($data)) {
$this->pointer[$this->index] = trim($data);
}

}

function _cleanString($string) {
return utf8_decode(trim($string));
}
}

$ch_counting = count($xmlC);


// estrae i dati dai nodi dell'xml
for ($i=0; $i<$ch_counting; $i++) {

$GLOBALS['datiposttrade'] = array();




$it_counting = count($xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] ->riga_potr);

for($j=0; $j < $it_counting; $j++) {

$GLOBALS['datiposttrade'][$j] = array();
$GLOBALS['datiposttrade'][$j]['prg_con'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> prg_con[0];
$GLOBALS['datiposttrade'][$j]['isin'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> isin[0];
$GLOBALS['datiposttrade'][$j]['qta'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> qta[0];
$GLOBALS['datiposttrade'][$j]['tqta'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> tqta[0];
$GLOBALS['datiposttrade'][$j]['prz'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> prz[0];
$GLOBALS['datiposttrade'][$j]['kdiv'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> kdiv[0];
$GLOBALS['datiposttrade'][$j]['dtese'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> dtese[0];
$GLOBALS['datiposttrade'][$j]['hhese'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> hhese[0];
$GLOBALS['datiposttrade'][$j]['tcor'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> tcor[0];
$GLOBALS['datiposttrade'][$j]['sede'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> sede[0];
$GLOBALS['datiposttrade'][$j]['ind'] = $xmlC -> obj_data -> datiposttrade[0] -> FUTLO[0] -> riga_potr[$j] -> ind[0];

Torav
04-09-2008, 16:46
oddio tutti quegli eval mi incasinano un po' il codice ma direi che ti basta aggiungere un $attrib tra i parametri della funzione:


function _startElement( $parser, $tag, $attrib )


e poi lo utilizzi come vuoi..metti che vuoi salvarti tutti gli attributi che stanno nel tag "prova", io farei una cosa del genere (tu adattalo ai tuoi bisogni magari):


if($tag == "prova" && count($attrib) > 0) {
foreach($attrib as $key => $value) {
$this->pointer[$this->index]->attribs[$key] = $value;
}
}


non ho mai usato molto eval quindi potrei aver sbagliato qualcosa con gli indici ma se sono riuscito a farti capire puoi correggere da solo :p

intel86
04-09-2008, 17:45
Per estrarre l'attributo des del tag isin in php devi usare questo codice:


$dom->getElementsByTagName("isin")->item(i-esimo)->attributes->getNamedItem("des")


Se non funziona fammi sapere ;) .. ciao ciao...