【问题标题】:Reading XML and storing to arrays in PHP在 PHP 中读取 XML 并存储到数组
【发布时间】:2011-08-11 09:01:50
【问题描述】:

我有这个:

$xml = simplexml_load_file('test.xml');
print"<pre>";
print_r($xml);

打印出来:

SimpleXMLElement Object
(
    [b] => SimpleXMLElement Object
        (
            [c] => SimpleXMLElement Object
                (
                    [d] => 543
                )

        )

)

但是当我输入 echo $xml["b"]["c"]["d"]; 时,什么也没有发生

【问题讨论】:

  • 如果你使用的是PHP5,我建议你使用SimpleXML的OO方式

标签: php xml arrays


【解决方案1】:

print_r 有点误导,
实际上 $xml 是 SimpleXmlElement 对象的系列/数组

所以

echo (int)$xml->b->c->d;  --- type casting is required

这里有一些reference,你应该先看看

除了type casting,
因为 xml 对象内的所有节点都是 stringint

PHP 会自动将数字字符串转换为整数,
但是,如果您提供类型提示会更清楚

var_dump($xml); --- you should see more information on the data type

【讨论】:

    【解决方案2】:

    试试echo $xml-&gt;b-&gt;c-&gt;d;

    $xml 不是数组,它们是对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多