【问题标题】:Read SimpleXmlElement object读取 SimpleXmlElement 对象
【发布时间】:2012-11-12 18:58:10
【问题描述】:

我有以下要解析的 xml。

Array
(
 [0] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [rel] => http://schemas.google.com/g/2005#other
                [address] => xyz@gmail.com
                [primary] => true
            )

    )

[1] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [rel] => http://schemas.google.com/g/2005#other
                [address] => abc@gmail.com
                [primary] => true
            )

    )
)

我有这个上面的 xml,我只需要从这个 xml 中获取地址。

foreach ($result as $title) {
   $email[$count++]=$title->attributes()->address->__toString; 
}
debug($email);

结果是这样的。但我只想要地址。需要一些帮助。

Array
(
[0] => SimpleXMLElement Object
    (
    )

[1] => SimpleXMLElement Object
    (
    )
)

【问题讨论】:

  • 这可能对$email[$count++]=(string)$title->attributes()->address;有帮助
  • 是的 .. 行得通...您可以将其添加为答案吗?

标签: php cakephp xml-parsing


【解决方案1】:

见:http://www.php.net/manual/en/simplexmlelement.attributes.php

返回值

返回一个 SimpleXMLElement 对象,可以对其进行迭代以遍历标签上的属性。

解决方案是将值转换为字符串,
例如:

$email[$count++]=(string)$title->attributes()->address;

或者迭代返回值也可以

例如:

foreach($title->attributes() as $key => $val)
{
  if ($key == 'address') $email[$count++] = $val;
}

【讨论】:

    猜你喜欢
    • 2020-01-04
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    相关资源
    最近更新 更多