简单的 xml php 需要帮助

发布于 2024-11-26 21:20:26 字数 2024 浏览 0 评论 0原文

我在使用简单的 xml 将其分配为变量时遇到问题。

这是我的代码:

$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

请帮忙。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

披肩女神 2024-12-03 21:20:26

您的 for 循环中有一个拼写错误: for($i=0;i<=$cnt;$i++) { 应该是 for($i=0;i<$cnt ;$i++) { (no =)

顺便说一句, $xml->item 支持 foreach,所以你可以这样做:

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

这有利于本地使用语言构造,它避免了需要count,并且完全避免了<=拼写错误的可能性。

另外,请注意,如果您有多个具有“id”属性的节点,则只有一个节点最终会出现在 $attr 中。如果这是您所需要的,那很好,但是如果您没有注意到它,就会导致头痛。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文