从php中的xml数据中获取值

时间:2016-04-14 22:51:57

标签: php xml

我以xml格式获取输出。我想获取每个<field> name值,如awb_number,order_id,actual_weight等。如何获取每个名称值。

 <ecomexpress-objects version="1.0">
    <object pk="1" model="awb">
    <field type="BigIntegerField" name="awb_number">700054480</field>
    <field type="CharField" name="orderid">5012</field>
    <field type="FloatField" name="actual_weight">0.5</field>
    <field type="CharField" name="origin">DELHI-DSW</field>
    <field type="CharField" name="destination">DELHI-DLN</field>
    <field type="CharField" name="customer">Ecom Express Private Limited - 32012</field>
    <field type="CharField" name="consignee">MUKESH KUMAR GUPTA</field>
    <field type="CharField" name="pickupdate">11-Feb-2013</field>
    <field type="CharField" name="status">Delivered / Closed</field>
    <field type="CharField" name="reason_code"/>
    <field type="CharField" name="reason_code_description"/>
    <field type="CharField" name="reason_code_number">999</field>
    <field type="CharField" name="receiver">mukesh 9999488339</field>
    <field type="CharField" name="expected_date">12-Feb-2013</field>
    <field type="CharField" name="last_update_date">05-Apr-2013</field>
    <field type="CharField" name="delivery_date">2013-02-17 11:26:00</field>
    <field type="CharField" name="ref_awb">None</field>
    <field type="CharField" name="rts_shipment"/>
    <field type="CharField" name="system_delivery_update">2013-02-17 11:26:00</field>
    <field type="CharField" name="rts_system_delivery_status"/>
    </object>
 </ecomexpress-objects>

我尝试了下面的代码,但是我无法从xml等显示文本awb_number,order_id,actual_weight

$xml = new SimpleXMLElement($server_output);
foreach($xml->object->field as $key =>$value)
{
    //foreach($element as $key => $value)
        echo($key .'-' .$value)."<br>";


}

1 个答案:

答案 0 :(得分:1)

name不是节点值,而是节点属性。要检索其值,您必须使用数组语法:

echo $value['name'];  // Output for first field: awb_number
echo $value;          // Output for first field: 700054480