嗨,你能帮我解析 XML 元素吗?我正在尝试从 XML 中获取值并将其写入 csv 文件,到目前为止,我可以读取所有内容,但 XML 中的内部元素让我感到困惑,所以如果你能给出解决方法的想法?
我有一个 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>
该数据只是示例,我需要将其中的任何内容写入 csv。这部分给我带来了问题。我不知道如何从这个 XML 文件中提取这些数据现在当涉及到这个脚本中断时,这就是问题所在并且在获取这些数据时遇到问题:
<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>
我需要保存该名称的名称和值
这是我的代码,但不适用于元素。那是缺失的,我需要弄清楚如何在这段代码中实现它:
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";
}