Hi Can you help me with parsing XML elements
I am trying to get values from XML and write it to csv file, so far I can read everything but inner elements in XML are confusing to me, so If you can give ideas how to solve this?
I have a XML:
<Product>
<ProductCode>51629AE</ProductCode>
<Vendor>HEWLETT PACKARD</Vendor>
<ProductType>Printer Ink Cartridge</ProductType>
<ProductCategory>Printeri</ProductCategory>
<ProductDescription>Ink Cartridge HEWLETT PACKARD Black 40ml for HP DJ600/660/670/680/690/DW 600/660/680/690/OJ500/600/700/PSC-370/380/FAX 910 (650pages)</ProductDescription>
<Image>https://www.it4profit.com/catalogimg/wic/1/51629AE</Image>
<ProductCard>https://content.it4profit.com/itshop/itemcard_cs.jsp?ITEM=50409104044691930&THEME=asbis&LANG=lv</ProductCard>
<AttrList>
<element Name="Ink Color" Value="Black"/>
<element Name="Ink Capacity" Value="40 ml"/>
<element Name="Compliance" Value="HP DJ600/660/670/680/690/DW 600/660/680/690/OJ500/600/700/PSC-370/380/FAX 910"/>
<element Name="Estimated Maximum Cartridge Life" Value="650 pages"/>
<element Name="Number of Ink Colors" Value="1"/>
<element Name="Typical Page Coverage" Value="5 %"/>
<element Name="Warranty Products returnable" Value="Yes"/>
<element Name="Pack Weight Brutto (kg)" Value="0.1 kg"/>
<element Name="Pieces in pack" Value="1"/>
</AttrList>
<MarketingInfo>
<element>The HP no. 29 black inkjet print cartridge is ideal for business or home users who want professional-quality printouts. The HP no. 29 is a 600-dpi black pigmented inkjet print cartridge that delivers sharp, professional quality documents and is made to work with HP colour inks for maximum results. Output is consistently crisp and clean on almost any plain paper. The print cartridge contains genuine HP ink that provides quality and reliability every time you print.</element>
</MarketingInfo>
<Images>
<Image>https://content.it4profit.com/pimg/s/resize/160x160x160x160/2783330.jpg</Image>
</Images>
</Product>
This data is just example I need to take whatever is there and write it to csv.
This part is making problems to me. I don't know how to extract this data from this XML file
Now when it comes to this script breaks and that is the problem
And have problems getting this data:
<AttrList>
<element Name="Ink Color" Value="Black"/>
<element Name="Ink Capacity" Value="40 ml"/>
<element Name="Compliance" Value="HP DJ600/660/670/680/690/DW 600/660/680/690/OJ500/600/700/PSC-370/380/FAX 910"/>
<element Name="Estimated Maximum Cartridge Life" Value="650 pages"/>
<element Name="Number of Ink Colors" Value="1"/>
<element Name="Typical Page Coverage" Value="5 %"/>
<element Name="Warranty Products returnable" Value="Yes"/>
<element Name="Pack Weight Brutto (kg)" Value="0.1 kg"/>
<element Name="Pieces in pack" Value="1"/>
</AttrList>
I need to save name and value for that name
This is my code that worsk but not for <AttrList> elements. That is missing and I need to figure out how to implement it in this code.
I need to add something like this to code:
foreach($xml->children() as $node) {
foreach ($node->AttrList->children() as $child) {
foreach($child->attributes() as $attribute) {
}
}
}
this is the code:
function parsing_xml($supplier_name,$xml_url,$opsi='string'){
$xml = simplexml_load_file(utf8_encode($xml_url), 'SimpleXMLElement', LIBXML_NOCDATA);
//$xml = simplexml_load_file($xml_url, 'SimpleXMLElement', LIBXML_NOCDATA);
if(!$xml){echo "xml $xml_url not loaded.";exit;}
$i=0;
$string="";
foreach($xml as $k=>$v){
$b="";
if($i==0){
foreach($v as $k1=>$v1){
$head[]=$k1;
}
array_push($head,"ime_proizvoda","meta_description","meta_tag","tax","supplier","quantity","price","Referenca","tezina","opis_link");
//array_push($head,"dugi_opis","ime_proizvoda","meta_description","meta_tag","tax","supplier","quantity","price","Referenca","tezina");
foreach($head as $k4=>$v4){
$b.= "$v4".DELIMITER;
}
}//echo $xml->$k->BrojArtikla."=>".$xml->$k->Image."<pre>".print_r($head,1)."</pre>";
foreach($head as $k3=>$v3){
if($v3=='meta_description'){$v3='Naziv';}
if(empty($v->$v3) or $v->$v3=="" or !isset($v->$v3)){
$v->$v3="|||";
}
if($v3=='tax'){
$b.=TAX.DELIMITER;
$v->$v3=TAX;
}elseif($v3=='supplier'){
$b.=$supplier_name.DELIMITER;
$v->$v3=$supplier_name;
}elseif($v3=='quantity'){
if($v->Stanje=="+"){
//if($v->Status=='Raspoloživo'){
$b.="4".DELIMITER;
$v->$v3=2;
}else{
$b.="0".DELIMITER;
$v->$v3=0;
}
}elseif($v3=='Cijena'){
//$b.=(string) floatval($v->$v3).DELIMITER;
//$v->$v3=(string) floatval($v->$v3);
$b.=str_replace(',', '.', (string) $v->$v3).DELIMITER;
$v->$v3=str_replace(',', '.', (string) $v->$v3);
//$b.= str_replace(',', '.', floatval($v->$v3)).DELIMITER;
//$v->$v3= str_replace(',', '.', floatval($v->$v3));
}elseif($v3=='ListPrice'){
$b.= (string) floatval($v->$v3).DELIMITER;
$v->$v3= (string) floatval($v->$v3);
}elseif($v3=='Rabat'){
$b.= (string) floatval($v->$v3).DELIMITER;
$v->$v3= (string) floatval($v->$v3);
}elseif($v3=='price'){
$b.= (string) floatval($v->Cijena)*MARZA.DELIMITER;
$v->$v3=(string) floatval($v->Cijena)*MARZA;
}
elseif($v3=='meta_tag'){
$b.=$v->$v->Brand.DELIMITER;
$v->$v3=$v->$v->Brand;
}
//ovo za ime da makne cudne znakove
elseif($v3=='ime_proizvoda'){
$b.=(string) preg_replace('/[%&#\\/-]/', '', $v->Naziv).DELIMITER;
//
//
//
$v->$v3=(string) preg_replace('/[%&#\\/-]/', '', $v->Naziv);
}
//ovo za referencu makni prva 4 znaka
elseif($v3=='Referenca'){
$b.=substr($v->Sifra, 4).DELIMITER;
$v->$v3=substr($v->Sifra, 4);
}
//ovo opis sa linkom
elseif($v3=='opis_link'){
$b.=$v->$v->Opis."<br/>Link na proizvod: <p><a href=$v->Link>$v->Link</a></p>".DELIMITER;
$v->$v3=$v->Opis."<br/>Link na proizvod: <p><a href=$v->Link>$v->Link</a></p>";
}
elseif($v3=='tezina'){
$b.=str_replace(',', '.', $v->Bruto_tezina_kg).DELIMITER;
$v->$v3=str_replace(',', '.', $v->Bruto_tezina_kg);;
}
else{
$b.=$v->$v3.DELIMITER;
}
}
$supplier_item[(string)$v->Sifra]=$v;
$b=substr($b,0,-1);
$b=str_replace("'","\'",$b);
$b=str_replace('"','\"',$b);
$b=str_replace("\n"," ",$b);
$i++;
$string.="\n$b";
}