duanhui7329 2016-04-15 00:37
浏览 40
已采纳

从php中的xml获取数据

I Want get each <object pk='1'>, <object pk='2'> and its <field> attribute like update_on and its each <field> text LIKE 05 Apr, 2013, 19:40 hrs but I can not fetch all data. please help me

I tried Below Code,below code fetch only first object's field

$xml = new SimpleXMLElement($server_output);
echo "<ul>";

foreach($xml->object->field as $key => $value)
{ ?>
<li style="list-style:none;"><?php echo $value['name'] .'-' ?> <span><?php echo $value; ?> </span><li>
<?php 
if($value['name'] == 'scans')
{

            print_r($value->object);


}
  }
 echo "</ul>";

Below Xml Code

<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"/>
        <field name="scans">
         <object pk="1" model="scan_stages">
          <field type="DateTimeField" name="updated_on">05 Apr, 2013, 19:40 hrs</field>
           <field type="CharField" name="status">SAL Tally closed</field>
           <field type="CharField" name="reason_code"/>
           <field type="CharField" name="reason_code_number"/>
           <field type="CharField" name="location">DLN</field>
           <field type="CharField" name="Employee">Umesh Jaswanti - 10015</field>
         </object>
         <object pk="2" model="scan_stages">
           <field type="DateTimeField" name="updated_on">05 Apr, 2013, 19:39 hrs</field>
           <field type="CharField" name="status">SAL Tally closed</field>
           <field type="CharField" name="reason_code"/>
           <field type="CharField" name="reason_code_number"/>
           <field type="CharField" name="location">DLN</field>
           <field type="CharField" name="Employee">Umesh Jaswanti - 10015</field>
         </object>
         <object pk="3" model="scan_stages">
           <field type="DateTimeField" name="updated_on">05 Apr, 2013, 19:39 hrs</field>
           <field type="CharField" name="status">SAL Tally closed</field>
           <field type="CharField" name="reason_code"/>
           <field type="CharField" name="reason_code_number"/>
           <field type="CharField" name="location">DLN</field>
           <field type="CharField" name="Employee">Umesh Jaswanti - 10015</field>
          </object>
        </field>
       </object>
</ecomexpress-objects>
  • 写回答

1条回答 默认 最新

  • dongqi6486 2016-04-16 00:04
    关注

    You have to use nested foreach:

    foreach( $xml->object->field as $key => $value )
    {
        (...)
        if( $value['name'] == 'scans' )
        {
            foreach( $value->object as $obj )
            {
                /* Here you can output <object> attributes */
                foreach( $obj->field as $field )
                {
                    /* Here you can output <field> attributes and values */
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?