2013-01-07 187 views
-1

嗨你能幫我解析XML元素 我想從XML獲取值並將其寫入到csv文件,到目前爲止我可以讀取所有東西,但XML中的內部元素讓我感到困惑,所以如果你可以給出想法如何解決這個問題?XML元素解析PHP

我有一個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&amp;THEME=asbis&amp;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"; 

     } 
+1

「這讓我感到困惑」不是我們可以解決的問題。你能否詳細解釋一下*真正的問題是什麼? – Charles

+2

另外,請通讀http://sscce.org/ - 這個例子太長了。 – Amadan

+0

好的,我已經編輯了問題,問題是我不知道如何從XML文件中獲得 mehnihma

回答

1

你應該能夠$element->attributes()得到一個SimpleXMLElement的屬性,這將返回一個關聯數組的屬性名稱和值,在你的榜樣:

"Name" => "Ink Color" 
"Value" => "Black" 

爲的SimpleXMLElement的文檔見http://php.net/manual/en/simplexmlelement.attributes.php

+0

您可以指導我如何實現它以使用此代碼? – mehnihma

+0

我可以添加類似: \t \t「的foreach($ XML->兒童()爲$節點){ \t \t \t的foreach($節點 - > AttrList->兒童()爲$子){ \t \t \t \t的foreach($兒童安全>屬性()作爲$屬性){ \t \t \t \t \t \t \t \t \t} \t \t \t} \t \t}' – mehnihma

+0

是的,據我所知,應該工作。已經有一段時間了,因爲我不得不這樣做,但是如果你不需要像移動或添加節點這樣的高級事物,SimpleXml非常簡單,並且是在PHP中處理XML的好選擇。 – Manuel