打印数组时SimpleXMLElement对象中的奇怪行为

时间:2015-06-14 16:59:44

标签: php xml simplexml php-5.5

我在我的SimpleXMLElement Object中与阵列挣扎。不知何故,当我打印数组$node->reference时,我没有得到预期的结果。

print_r($node);显示:

SimpleXMLElement Object
(
    [reference] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [resourceIdentifier] => 52chgb7f-1a00-4eaf-ac8a-5d4557f9796a
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [resourceIdentifier] => 52cbccc3-b754-4e88-9238-5d5257f9796a
                        )

                )

        )
)

print_r($node->reference);print_r($node->reference->children());显示:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [resourceIdentifier] => 52chgb7f-1a00-4eaf-ac8a-5d4557f9796a
        )

)

我希望看到:

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [resourceIdentifier] => 52chgb7f-1a00-4eaf-ac8a-5d4557f9796a
                )

        )

    [1] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [resourceIdentifier] => 52cbccc3-b754-4e88-9238-5d5257f9796a
                )

        )

)

修改

以下是一些要重现的代码:

<?php

$xml = '<?xml version="1.0" encoding="UTF-8" ?>
<items>
    <item>
        <reference resourceIdentifier="52chgb7f-1a00-4eaf-ac8a-5d4557f9796a" />
        <reference resourceIdentifier="52cbccc3-b754-4e88-9238-5d5257f9796a" />
    </item>
    <item>
        <reference resourceIdentifier="52chgb7f-1a00-4eaf-ac8a-5d4557f9796a" />
    </item>
    <item>
        <reference resourceIdentifier="52chgb7f-1a00-4eaf-ac8a-5d4557f9796a" />
        <reference resourceIdentifier="52chgb7f-1a00-4eaf-ac8a-5d4557f9796a" />
        <reference resourceIdentifier="52cbccc3-b754-4e88-9238-5d5257f9796a" />
    </item>
</items>';

$items = new \SimpleXMLElement($xml);
foreach ($items as $item) {

    echo '<h1>Item</h1>';
    echo '<pre>';
    print_r($item);
    print_r($item->reference); // Returns always 1 SimpleXMLElement Object?
    print_r($item->reference->children()); // Returns always 1 SimpleXMLElement Object?
    echo '</pre>';

}

1 个答案:

答案 0 :(得分:3)

简单的答案是:不要依赖print_r。 SimpleXML的用处在于它使用了很多,因为缺少更好的单词,“魔术”,print_r(和var_dumpvar_export以及几乎任何其他通用调试或序列化函数)没有告诉你它将如何表现。此外,这非常重要, SimpleXMLElement不包含任何数组

我写了a dedicated debug function,虽然不完美,但是通过SimpleXML对象比原生对象更好。

此特定行为的原因是您可以使用$node->reference来引用 所有名为reference的孩子的列表第一个这样的孩子。以下都是等效的:

// Access as iterable list
foreach ( $node->reference as $ref ) {
    echo $ref['resourceIdentifier'];
    // only loop once
    break;
}

// Access as numerically indexed array
echo $node->reference[0]['resourceIdentifier'];

// Access first item by default
echo $node->reference['resourceIdentifier'];

如果您的文档“深而窄”,这非常方便,例如

$xml = simplexml_load_string('<foo><bar><baz><quux hello="world" /></baz></bar></foo>');
echo $xml->bar->baz->quux['hello']; // world

SimpleXML只允许您编写这样的表达式并忽略任何倍数,而不是必须检查节点是唯一节点还是多节点:

$xml = simplexml_load_string('<foo><bar><baz><quux hello="world" /><quux ignored="true" /></baz></bar><bar>ignored</bar></foo>');
echo $xml->bar->baz->quux['hello']; // world