我已生成一个具有以下功能的xml文件。
$orderInfo = Array
(
[order_number] => MI100000038
[customer_id] => 645
[customer_email] => test@yopmail.com
[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');
现在,我想再次使用此过程并将数据附加到退出的order.xml文件。我使用下面的代码:
if (file_exists($xml_order_info_file)) {
echo '<br/>in';
$xml = simplexml_load_string($xml_order_info_file);
但它不适合我。
答案 0 :(得分: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);