0
我遇到了簡單的XML的問題,把它分配爲變量。簡單的XML PHP需要幫助
這裏是我的代碼:
$config = '../XML/config.xml';
$xml = simplexml_load_file($config);
$cnt = count($xml->children());
for($i=0;i<=$cnt;$i++) {
foreach($xml->item[$i]->attributes() as $a => $b) {
echo $a."<br />"; //result
}
}
這裏是我的XML:
<?xml version="1.0" encoding="UTF-8"?>
<portfolio tooltip="click to view">
<item path="approvals/710.png" title="ASC Approval" description="Oct 25, 2010 approval letter from ASC." link="approvals/710.PDF" target="_blank" />
<item path="approvals/711.png" title="Citi Approval" description="Nov 1, 2010 approval letter from CitiMortgage." link="approvals/711.PDF" target="_blank" />
<item path="approvals/712.png" title="Citi/Freddie" description="Nov 9, 2010 approval letter from Freddie Mac for CitiMortgage." link="approvals/712.PDF" target="_blank" />
<item path="approvals/713.png" title="BoA Approval" description="Nov 9, 2010 approval letter from Bank of America." link="approvals/713.PDF" target="_blank" />
<item path="approvals/714.png" title="Pentagon" description="Nov 10, 2010 approval letter from Pentagon FCU." link="approvals/714.PDF" target="_blank" />
<item path="approvals/715.png" title="PNC Approval" description="Nov 10, 2010 approval letter from PNC Mortgage." link="approvals/715.PDF" target="_blank" />
</portfolio>
我需要的結果在一個變量的形式。我嘗試這一個,並得到了一個錯誤
$config = '../XML/config.xml';
$attr = array();
$xml = simplexml_load_file($config);
$cnt = count($xml->children());
for($i=0;i<=$cnt;$i++) {
foreach($xml->item[$i]->attributes() as $a => $b) {
$attr[$a] = $b;
}
}
echo "<pre>";
print_r($attr);
echo "</pre>";
但我得到這個錯誤
Fatal error: Call to a member function attributes() on a non-object in E:\xampp\htdocs\slide\admin\admin.php on line 51
請幫助。由於
你能發佈你的XML嗎? –
xml發佈! :)。 – Jorge