简体   繁体   English

简单的xml php需要帮助

[英]simple xml php need help

I'm having trouble with simple xml to assign it as variable. 我在使用简单的xml将其分配为变量时遇到麻烦。

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:

<?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 谢谢

You've a typo in your for loop: for($i=0;i<=$cnt;$i++) { should be for($i=0;i<$cnt;$i++) { (no = ) 您在for循环中有错别字: for($i=0;i<=$cnt;$i++) {应该是for($i=0;i<$cnt;$i++) { (no =

As an aside, $xml->item supports foreach, so you could do: 顺便说一句,$ xml-> item支持foreach,因此您可以执行以下操作:

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. 这具有使用本机语言构造的好处,它避免了对count的需求,并且完全避免了<=错字的可能性。

Also, just as a heads up, if you have multiple nodes with an "id" attribute, only one will end up in $attr . 另外,就像一个提示一样,如果您有多个具有“ id”属性的节点,则只有一个节点以$attr结尾。 If this is what you need, that's fine, but it is something which can cause headache if you don't notice it. 如果这是您需要的,那很好,但是如果您不注意它,可能会引起头痛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM