使用PHP重新组合/重命名XML

时间:2016-04-05 12:53:32

标签: php xml

我有一个像这样的XML结构:

<OrderLineItems>
<SKU/>
<ItemName>ÁFONYÁS FÁNK</ItemName>
<Quantity>1</Quantity>
<Price>350</Price>
<LineTotal>350</LineTotal>
<Meta/>
</OrderLineItems>
<OrderLineItems>
<SKU/>
<ItemName>CSIRKE SALÁTA ÖNTETTEL</ItemName>
<Quantity>1</Quantity>
<Price>1150</Price>
<LineTotal>1150</LineTotal>
<Meta/>
</OrderLineItems>

我希望输出如下:

<basket>
<item> ->OrderLineItems
<billitem_code></billitem_code> -> SKU
<billitem_name></billitem_name>  -> this will be the ItemName
<billitem_amount></billitem_amount> ->Quantity
<billitem_unitprice></billitem_unitprice> ->Price
</item>
</basket>

我设法读取了XML并输出了一些基本的行

$xml -> customer -> name = $orderxml -> Order -> BillingFullName;

但如果超过1,我就无法查看订购的商品。

有关如何管理此建议的任何建议吗?

当前代码:

 <?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
header("Content-type:text/xml");

//Load
require "apiclass/database.php";
define("DEFAULT_XML_FILE",dirname(__FILE__).'/default.xml');

//Database 
$db = new Database;

//get XML
$order_id = $_GET["order_id"];
$order_file = "xml/order_id-".$order_id.".xml";

//XML 
$orderxml = simplexml_load_file($order_file);
$customerid = $orderxml -> Order -> CustomerId;

$xml = simplexml_load_file(DEFAULT_XML_FILE);

//Converting loaded XML to output format
$xml -> customer -> id = $orderxml -> Order -> CustomerId;
$xml -> orderdate = $orderxml -> Order -> OrderDate;
$xml -> customer -> name = $orderxml -> Order -> BillingFullName;
$xml -> customer -> tel = $orderxml -> Order -> BillingPhone;
$xml -> customer -> address -> postalcode = $orderxml -> Order -> BillingPostCode;
$xml -> customer -> address -> city = $orderxml -> Order -> BillingCity;
$xml -> customer -> address -> district = $orderxml -> Order -> BillingState;
$xml -> customer -> address -> street = $orderxml -> Order -> BillingAddress1;
$xml -> customer -> address -> number = $orderxml -> Order -> BillingAddress2;
$xml -> customer -> address -> description = $orderxml -> Order -> CustomerNote;
$xml -> payment_method = $orderxml -> Order -> ShippingMethod;

foreach($orderxml -> Order -> OrderLineItems -> ItemName as $key => $value)
{
$xml -> basket -> item -> billitem_name =  $value ;
}

$xml -> deliveryprice = $orderxml -> Order -> FeeTotal;
$xml -> totalprice = $orderxml -> Order -> OrderTotal;
print $xml -> asXML();

//Debuggolás
//echo '<pre>';
//var_dump($xml);
//echo '</pre>';
?>

1 个答案:

答案 0 :(得分:0)

转换XML的最佳方法可能是XSLT,这是一种将XML文档转换为其他XML文档的语言。此外,DOMDocument比SimpleXML对导入/转换节点更强大。

使用SimpleXML,您可以使用->addChild(name,value)->addChild(name,value)->...

否则,您可以使用技巧。将目标XML模板分为两部分:

$xmlDefault ='<?xml version="1.0" encoding="utf-8"?>
<basket/>';

$itemTemplate = '<item>
<billitem_code></billitem_code>
<billitem_name></billitem_name>
<billitem_amount></billitem_amount>
<billitem_unitprice></billitem_unitprice>
</item>
';

加载源XML文件:

$orderxml       = simplexml_load_file( $order_file );

选择<OrderLineItems>元素:

$orderLineItems = $orderxml->OrderLineItems;

然后创建具有相同<item>$orderLineItems的新目标XML:

$newXml = "<basket>\n".str_repeat( $itemTemplate, count( $orderLineItems ) )."</basket>";
$xml = simplexml_load_string( $newXml );

现在您可以添加每个节点:

$key = 0;
foreach( $orderLineItems as $node )
{
    $xml->item[$key]->billitem_code      = $node->SKU;
    $xml->item[$key]->billitem_name      = $node->ItemName;
    $xml->item[$key]->billitem_amount    = $node->Quantity;
    $xml->item[$key]->billitem_unitprice = $node->Price;
    $key++;
}

$xml最终内容:

<basket>
    <item>
        <billitem_code></billitem_code>
        <billitem_name>ÁFONYÁS FÁNK</billitem_name>
        <billitem_amount>1</billitem_amount>
        <billitem_unitprice>350</billitem_unitprice>
    </item>
    <item>
        <billitem_code></billitem_code>
        <billitem_name>CSIRKE SALÁTA ÖNTETTEL</billitem_name>
        <billitem_amount>1</billitem_amount>
        <billitem_unitprice>1150</billitem_unitprice>
    </item>
</basket>