2016-03-28 12 views
0

以下の関数を使ってxmlファイルを生成しました。php - 配列をxmlにしてデータをexting xmlファイルに追加する

$orderInfo = Array 
    (
    [order_number] => MI100000038 
    [customer_id] => 645 
    [customer_email] => [email protected] 
     [protect_code] => xxxx33639 
    [total_qty_ordered] => 4.0000 
    [created_at] => 2016-03-25 20:01:05 
    [billing_address] => Array 
    (
     [street] => Array 
      (
       [0] => xxx 
      ) 

     [city] => xx 
     [region] => xx 
     [postcode] => 848151 
     [country_id] => x 
     [telephone] => 8745165 
    ) 

    [items] => Array 
    (
     [1] => Array 
      (
       [name] => fgfgf 
       [sku] => 796 
       [qty_ordered] => 3.0000 
       [price] => 81.2000 
       [product_type] => simple 
      ) 
    ) 

    ) ; 

    $xml_order_info = new SimpleXMLElement('<?xml version=\'1.0\'?><order_info></order_info>"); 

    $this->array_to_xml($orderInfo,$xml_order_info); 

    function array_to_xml($array, &$xml_order_info) { 
    foreach($array as $key => $value) { 
     if(is_array($value)) { 
      if(!is_numeric($key)){ 
       $subnode = $xml_order_info->addChild("$key"); 
       $this->array_to_xml($value, $subnode); 
      }else{ 
       $subnode = $xml_order_info->addChild("item$key"); 
       $this->array_to_xml($value, $subnode); 
      } 
     }else { 
      $xml_order_info->addChild("$key",htmlspecialchars("$value")); 
     } 
    } 
} 

    $xml_file = $xml_order_info->asXML('order.xml'); 

ここで、このプロセスをもう一度使用して、データをexiting order.xmlファイルに追加します。私は以下のコードを使用します:

if (file_exists($xml_order_info_file)) { 
     echo '<br/>in'; 
     $xml = simplexml_load_string($xml_order_info_file); 

しかし、私のために働いていません。

答えて

0

simplexml_load_stringは、パラメータとしてファイル名を期待するが、唯一の整形式XML文字列はありません:

整形式のXML文字列を取り、オブジェクトとして返します。

だから、より良い代わりにsimplexml_load_fileを使用します。

if (file_exists($xml_order_info_file)) { 
    echo '<br/>in'; 
    $xml = simplexml_load_file($xml_order_info_file); 
関連する問題