XML打印额外节点

时间:2014-04-08 18:08:48

标签: xml xmlnode

我正在做一个相当简单的事情,但遗憾的是我无法按照我想要的方式解决它。这是我的代码

$orderline_key = 0;
for ($x = 0; $x < $orderline_val->items_total; $x++) {
    $product_sku = $orderline_val->products[$x]->sku;
    for ($quantity = 0; $quantity < $orderline_val->products[$x]->quantity; $quantity++)     {
        if (in_array($product_sku, $products_in_brand, true)) {

            if ($product_sku != '') {
                $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->VENDORITEMNUMBER[$orderline_key] = $product_sku;
            }
            if ($order_line != '') {
                $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->LINENUMBER[$orderline_key] = $order_line; // $orderline_val['LINENUMBER']; // "1";
            }
            if ($order_line != '') {
                $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->DESCRIPTION[$orderline_key] = $orderline_val->products[$x]->sku; // "50 Longstems Roses";
            }
            if ($order_line != '') {
                $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->QUANTITY[$orderline_key] = "1"; // $orderline_val['QUANTITY']; // "1"; Always One
            }
            if ($order_line != '') {
                $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->UNITPRICE[$orderline_key] = $orderline_val->products[$x]->base_price; // "49.99";
            }
            if ($order_line != '') {
                $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->TAX[$orderline_key] = number_format($orderline_val->products[0]->price_tax, 2); // number_format($orderline_val['UNITPRICE'] * $orderline_val['TAX'],2); // "2.50";
            }
            if ($order_line != '') {
                $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->SUBTOTAL[$orderline_key] = $orderline_val->products[$x]->total_inc_tax; // $orderline_val['SUBTOTAL']; // "49.99";
            }
            if ($order_line != '')
            {
                $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->SHIPPING[$orderline_key] = $orderline_val->products[$x]->fixed_shipping_cost / $orderline_val->items_total; // "25.00"; // How to handle w/ multiple items ????????????????
            }
            if ($order_line != '')
            {
                $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->TOTAL[$orderline_key] = $orderline_val->products[$x]->total_inc_tax;
            }
            $piped_str = $orderline_val->products[$x]->product_options[2]->value
            if($piped_str!='')
            {
            $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->CARDMESSAGE->MESSAGE[$orderline_key] = $piped_str; // 
            }
            $order_line++;
            $orderline_key++;
        }
    }
}

这是我得到的输出

<ORDERLINE>
    <VENDORITEMNUMBER>FRP130ASS</VENDORITEMNUMBER>
    <LINENUMBER>1</LINENUMBER>
    <DESCRIPTION>FRP130</DESCRIPTION>
    <QUANTITY>1</QUANTITY>
    <UNITPRICE>309.0000</UNITPRICE>
    <TAX>0.00</TAX>
    <SUBTOTAL>618.0000</SUBTOTAL>
    <SHIPPING>0</SHIPPING>
    <TOTAL>618.0000</TOTAL>
    <CARDMESSAGE>
        <MESSAGE></MESSAGE>
    </CARDMESSAGE>
</ORDERLINE>
<ORDERLINE>
    <VENDORITEMNUMBER/>
    <LINENUMBER/>
    <DESCRIPTION/>
    <QUANTITY/>
    <UNITPRICE/>
    <TAX/>
    <SUBTOTAL/>
    <SHIPPING/>
    <TOTAL/>
    <CARDMESSAGE>
        <MESSAGE/>
        <MESSAGE></MESSAGE>
    </CARDMESSAGE>
    <VENDORITEMNUMBER>FRP130ASS</VENDORITEMNUMBER>
    <LINENUMBER>2</LINENUMBER>
    <DESCRIPTION>FRP130</DESCRIPTION>
    <QUANTITY>1</QUANTITY>
    <UNITPRICE>309.0000</UNITPRICE>
    <TAX>0.00</TAX>
    <SUBTOTAL>618.0000</SUBTOTAL>
    <SHIPPING>0</SHIPPING>
    <TOTAL>618.0000</TOTAL>
</ORDERLINE>

你会看到我也会打印出很多额外的标签。我甚至提出了一个空的检查,如果它是空的,那么跳过它但它仍然添加那个空节点。

我出错的任何想法?

1 个答案:

答案 0 :(得分:0)

似乎在某些时候,此指令中的piped_str不为空。

if($piped_str!='')
{
 $xml->ORDERREQUEST->ORDER->ORDERLINE[$orderline_key]->CARDMESSAGE->MESSAGE[$orderline_key] = $piped_str;
}

这可能是因为条件$quantity < $orderline_val->products[$x]->quantity

的第二个因素

验证这里是否没有空数组元素。