读取 XML 数据到 php
Reading XML data to php
我有一个包含一组记录的 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 />';
}
}
我试过这个:
$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 />';
}
我有一个包含一组记录的 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 />';
}
}
我试过这个:
$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 />';
}