doxzrb8721 2011-07-27 01:56
浏览 44
已采纳

简单的xml php需要帮助

I'm having trouble with simple xml to assign it as variable.

Here is my code:

$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

    }
}

Here is my 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>

I need the result in a variable form. I try this one and got an error

$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>";

But I got this error

Fatal error: Call to a member function attributes() on a non-object in E:\xampp\htdocs\slide\admin\admin.php on line 51

Please help. Thanks

  • 写回答

1条回答 默认 最新

  • douxianwu2221 2011-07-27 02:46
    关注

    You've a typo in your for loop: for($i=0;i<=$cnt;$i++) { should be for($i=0;i<$cnt;$i++) { (no =)

    As an aside, $xml->item supports foreach, so you could do:

    foreach( $xml->item as $item )
    {
        foreach( $item->attributes() as $a => $b )
        {
            $attr[ $a ] = $b;
        }
    }
    

    This has the benefit of working with the language constructs natively, it avoids the need for count, and it completely avoids the possibility of a <= typo.

    Also, just as a heads up, if you have multiple nodes with an "id" attribute, only one will end up in $attr. If this is what you need, that's fine, but it is something which can cause headache if you don't notice it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题