dongsong4418 2015-01-09 13:56
浏览 17
已采纳

将XML数据读取到php

I am having a xml file which is having set of records ,And I want to load these records into php I am trying to pass these values to php but my code is not working .can any one tell me how to parse these xml data into php

Here is my xml data:

 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
- <report>
  <report-name name="Criteria performance report #54afd4b9c3fd9" /> 
  <date-range date="Dec 1, 2014-Dec 31, 2014" /> 
- <table>
- <columns>
  <column name="day" display="Day" /> 
  <column name="adGroupID" display="Ad group ID" /> 
  <column name="adGroup" display="Ad group" /> 
  <column name="ad" display="Ad" /> 
  <column name="descriptionLine1" display="Description line 1" /> 
  <column name="descriptionLine2" display="Description line 2" /> 
  <column name="displayURL" display="Display URL" /> 
  <column name="clicks" display="Clicks" /> 
  <column name="convertedClicks" display="Converted clicks" /> 
  <column name="cost" display="Cost" /> 
  <column name="ctr" display="CTR" /> 
  </columns>
  <row day="2014-12-01" adGroupID="16385367652" adGroup="Urology" ad="Best Urology Treatment" descriptionLine1="Get treated in top urology hospital" descriptionLine2="Take a free advice from our experts" displayURL="www.ainuindia.com" clicks="0" convertedClicks="0" cost="0" ctr="0.00%" /> 
  <row day="2014-12-01" adGroupID="16282238572" adGroup="Kidney Stones Removal" ad="Kidney Stone Removal" descriptionLine1="Get treated at top kidney center" descriptionLine2="Take a free advice from our experts" displayURL="www.ainuindia.com" clicks="0" convertedClicks="0" cost="0" ctr="0.00%" /> 
  <row day="2014-12-01" adGroupID="16282238572" adGroup="Kidney Stones Removal" ad="Kidney Stone Removal" descriptionLine1="Get treated at top kidney center" descriptionLine2="Take a free advice from our experts" displayURL="www.ainuindia.com" clicks="0" convertedClicks="0" cost="0" ctr="0.00%" /> 
</table>
 </report>

And my php code is :

if( ! $xml = simplexml_load_file(dirname(__FILE__) . '/report.xml') )
    {
        echo 'unable to load XML file';
    }
    else
    {
        foreach( $xml as $table )
        {
            echo 'Adgroup id: '.$table->columns->adGroupID.'<br />';
            echo 'group: '.$table->columns->adGroup.'<br />';
            echo 'Ad: '.$table->columns->ad.'<br />';
            echo 'Description1: '.  $table->columns->descriptionLine1.'<br />';
            echo 'Description2: '.$table->columns->descriptionLine2.'<br />';

        }
    } 
  • 写回答

1条回答 默认 最新

  • duan0424 2015-01-09 14:17
    关注

    i tried this:

    $string='<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
     <report>
      <report-name name="Criteria performance report #54afd4b9c3fd9" /> 
      <date-range date="Dec 1, 2014-Dec 31, 2014" /> 
     <table>
     <columns>
      <column name="day" display="Day" /> 
      <column name="adGroupID" display="Ad group ID" /> 
      <column name="adGroup" display="Ad group" /> 
      <column name="ad" display="Ad" /> 
      <column name="descriptionLine1" display="Description line 1" /> 
      <column name="descriptionLine2" display="Description line 2" /> 
      <column name="displayURL" display="Display URL" /> 
      <column name="clicks" display="Clicks" /> 
      <column name="convertedClicks" display="Converted clicks" /> 
      <column name="cost" display="Cost" /> 
      <column name="ctr" display="CTR" /> 
      </columns>
      <row day="2014-12-01" adGroupID="16385367652" adGroup="Urology" ad="Best Urology Treatment" descriptionLine1="Get treated in top urology hospital" descriptionLine2="Take a free advice from our experts" displayURL="www.ainuindia.com" clicks="0" convertedClicks="0" cost="0" ctr="0.00%" /> 
      <row day="2014-12-01" adGroupID="16282238572" adGroup="Kidney Stones Removal" ad="Kidney Stone Removal" descriptionLine1="Get treated at top kidney center" descriptionLine2="Take a free advice from our experts" displayURL="www.ainuindia.com" clicks="0" convertedClicks="0" cost="0" ctr="0.00%" /> 
      <row day="2014-12-01" adGroupID="16282238572" adGroup="Kidney Stones Removal" ad="Kidney Stone Removal" descriptionLine1="Get treated at top kidney center" descriptionLine2="Take a free advice from our experts" displayURL="www.ainuindia.com" clicks="0" convertedClicks="0" cost="0" ctr="0.00%" /> 
    </table>
     </report>';
    
    $xml = new SimpleXMLElement($string);//test
    //$xml = simplexml_load_file(dirname(__FILE__) . '/report.xml')
    
    foreach($xml->table->row as $r){
        echo 'Adgroup id: '.$r['adGroupID'].'<br />';
        echo 'group: '.$r['adGroup'].'<br />';
        echo 'Ad: '.$r['ad'].'<br />';
        echo 'Description1: '.  $r['descriptionLine1'].'<br />';
        echo 'Description2: '.$r['descriptionLine2'].'<br /><br />';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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