У меня есть 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 />';
}
Других решений пока нет …