How to get EAN number from below XML?
<offer>
...
<property EAN="5900017361000"/>
...
</offer>
My code does not bring any value.
$ean = $xpath->query( "//attribute::*[contains(., 'EAN')]" )->item(0)->nodeValue;
Whole script looks like this:
<?php
$xml = file_get_contents("url to remote feed");
libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->standalone=true;
$dom->strictErrorChecking=false;
$dom->recover=true;
$dom->formatOutput=false;
$dom->loadXML( $xml );
$errors=serialize( libxml_get_last_error() );
libxml_clear_errors();
$cats=$dom->getElementsByTagName('offer');
$xpath = new DOMXPath($dom);
// Register the php: namespace (required)
$xpath->registerNamespace("php", "http://php.net/xpath");
// Register PHP functions (no restrictions)
$xpath->registerPHPFunctions();
if( !empty( $cats ) && $cats->length > 0 ){
/* column headers for use in csv */
$columns = array(
'Identyfikator',
'Ilość',
'Nazwa',
'VAT',
'Cena zakupu',
'Cena zakupu netto',
'Cena sprzedaży netto',
'Cena sprzedaży brutto',
'EAN',
'Marka',
);
/* output file location */
$file=__DIR__ . '/nowyprodukt.csv';
/* placeholder array to store results for later writing to csv */
$data=array();
foreach( $cats as $cat ){
ini_set('max_execution_time', 600); //300 seconds = 5 minutes
set_time_limit(600);
$item=$cat->childNodes->item(1);
$identyfikator=trim( $cat->childNodes->item(1)->nodeValue );
$magazyn = $cat->getElementsByTagName("onstock")->item(0)->nodeValue;
$nazwa = $cat->getElementsByTagName("name")->item(0)->nodeValue;
$vat = $cat->getElementsByTagName("vat")->item(0)->nodeValue;
$cenazakupu = $cat->getElementsByTagName("price")->item(0)->nodeValue;
$cenazakupunetto = round($cenazakupu / (100 +$vat) *100, 2);
$ean = $xpath->query( "//attribute::*[contains(., 'EAN')]" )->item(0)->nodeValue;
$marka = $cat->getElementsByTagName("ext_marka")->item(0)->nodeValue;
$cenasprzedazynetto = number_format(round($cenazakupunetto, 2) * $margin, 2);
if (strpos($vat, '8') !== false) {
$podatek = number_format(round($cenasprzedazynetto, 2) * 1.08, 2);
$podatek_numer = "2";
}else{
$podatek = number_format(round($cenasprzedazynetto, 2) * 1.23, 2);
$podatek_numer = "1";
}
/*
Generate an array with the values found from querying the XML
*/
$data[]=array(
$columns[0] => $identyfikator,
$columns[1] => $magazyn,
$columns[2] => $nazwa,
$columns[3] => $podatek_numer,
$columns[4] => $cenazakupu,
$columns[5] => $cenazakupunetto,
$columns[6] => $cenasprzedazynetto,
$columns[7] => $podatek,
$columns[8] => $ean,
$columns[9] => $marka,
);
}
/*
Write the headers and csv data to file
*/
if( !empty( $data ) ){
$f = fopen( $file, 'w' );
fputcsv( $f, $columns );
foreach($data as $arr)fputcsv($f,$arr);
fclose($f);
echo count( $data ) . ' wierszy zapisanych w ' . $file;
}
}
$dom=null;
?>
Everything works fine, except EAN due to its strange structure.
Can you please help me?
...->item(0)->nodeValueshould be->getAttribute('ean'), nodeValue is the value of the node itself.->item(0), as the error says you're callinggetAttruibuteon the list and not the node.