将XML数据读入php

将XML数据读入php,php,xml,google-ads-api,Php,Xml,Google Ads Api,我有一个xml文件,其中包含一组记录,我想将这些记录加载到php中 我试图将这些值传递给php,但我的代码不起作用。有人能告诉我如何将这些xml数据解析为php吗 以下是我的xml数据: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> - <report> <report-name name="Criteria performance report #54afd4b9c3fd9" />

我有一个xml文件,其中包含一组记录,我想将这些记录加载到php中 我试图将这些值传递给php,但我的代码不起作用。有人能告诉我如何将这些xml数据解析为php吗

以下是我的xml数据:

 <?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>

- 
- 
- 
我的php代码是:

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 />';

        }
    } 
if(!$xml=simplexml\u load\u文件(dirname(\u文件).'/report.xml'))
{
echo“无法加载XML文件”;
}
其他的
{
foreach($xml作为$table)
{
回显“adGroupID:”。$table->columns->adGroupID。“
”; echo“group:”.$table->columns->adGroup.
; 回显“Ad:”.$table->columns->Ad.
; 回显“Description1:”。$table->columns->descriptionLine1。“
”; 回显“Description2:”。$table->columns->descriptionLine2。“
”; } }
我尝试了以下方法:

$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 />';
}
$string='1!'
';
$xml=新的simplexmlement($string)//测试
//$xml=simplexml\u load\u文件(dirname(\u文件)'/report.xml')
foreach($xml->table->row as$r){
回显'adGroupID:'.$r['adGroupID'].
; echo“group:”.$r['adGroup'].
; 回音'Ad:'.$r['Ad'].
; 回显“Description1:”.$r['descriptionLine1']。
; 回显“Description2:”.$r['descriptionLine2']。

; }
您打开通知显示并调试了吗?@shobhanbau您可以执行以下操作:
$string=file\u get\u contents(dirname(\u file\u)。'/report.xml')从文件加载内容