0

I have generate one xml file with below function.

  $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');

Now, I want to again use this process and append data to exiting order.xml file. I use below code:

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

But its not working for me.

1 Answer 1

0

simplexml_load_string does not expect a filename as parameter, but only a well-formed XML string:

Takes a well-formed XML string and returns it as an object.

So better use simplexml_load_file instead:

if (file_exists($xml_order_info_file)) {
    echo '<br/>in';
    $xml = simplexml_load_file($xml_order_info_file);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.