0

我有一个xml文件如下:

<response uri="/crm/private/xml/SalesOrders/getSearchRecordsByPDC"><result><SalesOrders><row no="1"><FL val="SALESORDERID">580005000000187001</FL><FL val="SO Number">580005000000187002</FL><FL val="Subject">Sales order test from sri 02</FL><FL val="Due Date">2013-03-10</FL><FL val="Carrier">FedEX</FL><FL val="Status">Created</FL><FL val="ACCOUNTID">580005000000088096</FL><FL val="Account Name">Best Western Pavilions</FL><FL val="SMOWNERID">580005000000052003</FL><FL val="Sales Order Owner">Adithya Buddhavarapu</FL><FL val="SMCREATORID">580005000000052003</FL><FL val="Created By">Adithya Buddhavarapu</FL><FL val="MODIFIEDBY">580005000000052003</FL><FL val="Modified By">Adithya Buddhavarapu</FL><FL val="Created Time">2013-01-17 20:32:19</FL><FL val="Modified Time">2013-02-05 07:37:33</FL><FL val="Sub Total">250</FL><FL val="Tax">0</FL><FL val="Adjustment">0</FL><FL val="Grand Total">250</FL><FL val="Product Details"><product no="1"><FL val="Product Id">580005000000171125</FL><FL val="Product Name">The Grilling Maestro</FL><FL val="Unit Price">10.0</FL><FL val="Quantity">20.0</FL><FL val="Quantity in Stock">0.0</FL><FL val="Total">200.0</FL><FL val="Discount">0.0</FL><FL val="Total After Discount">200.0</FL><FL val="List Price">10.0</FL><FL val="Net Total">200.0</FL><FL val="Tax">0.0</FL><FL val="Product Description">null</FL></product><product no="2"><FL val="Product Id">580005000000171131</FL><FL val="Product Name">Uptown Deli   Soup du Jour</FL><FL val="Unit Price">25.0</FL><FL val="Quantity">1.0</FL><FL val="Quantity in Stock">0.0</FL><FL val="Total">50.0</FL><FL val="Discount">1.23</FL><FL val="Total After Discount">23.77</FL><FL val="List Price">25.0</FL><FL val="Net Total">48.77</FL><FL val="Tax">0.0</FL><FL val="Product Description">null</FL></product></FL><FL val="Terms and Conditions">iBanquet T and C</FL><FL val="Description">Sales Order from iBanquet</FL><FL val="Discount">0</FL></row></SalesOrders></result></response>

从这个 xml 文件中,我需要形成一个包含键/值对的数组,例如array(SALESORDERID => '580005000000187001' ,SO Number => '580005000000187002', ....总计 => '250',Product Details => array([0]=>(Product Id =>'580005000000171125',Product Name => 'The Grilling Maestro',....,Product Description =>'null'),[1]=>(Product Id => '580005000000171131',....,Product Description =>'null')

我做了一个尝试:

$xml = simplexml_load_file($file_sales_content,'SimpleXMLElement', LIBXML_NOCDATA);
$order_array = array();
//store the Leads record into session according to the first name
foreach($xml->result->SalesOrders->row as $na){
array_push($order_array,$na->FL[0].":".$na->FL[1]."=".$na->FL[2]."^".$na->FL[3]."$".$na->FL[4]);
} 

在哪里$file_sales_content存储 xml 值。但是在这段代码中,我只能捕获值而不是键。

有谁能够帮助我。谢谢。

4

2 回答 2

0
$file_sales_content = "test.xml";
$i=0;
$xml = simplexml_load_file($file_sales_content);
$order_array = array();
foreach($xml->result->SalesOrders->row->FL as $na){
    $tag = (String)$na->attributes();
    $order_array[$tag] = (String)$na;
    if($tag == "Product Details"){            
        foreach($na->product as $na_prd){
            $i++;
            foreach($na_prd->FL as $prd_Acc){
                $order_array[$i][(String)$prd_Acc->attributes()] = (String)$prd_Acc;
            }                
        }
    }
}
print_r($order_array);
于 2013-02-05T08:45:30.213 回答
0

您的代码很接近,但您实际上可以更深入地迭代一个级别。

SimpleXmlElement此外,可以使用[]符号检索a 的属性。

$output = array();
foreach($xml->result->SalesOrders->row->FL as $fl) {
    $output[$fl['val']] = (string)$fl;
}
于 2013-02-05T08:48:19.423 回答