读取SimpleXmlElement对象

时间:2012-11-12 18:58:10

标签: php cakephp xml-parsing

我要解析以下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
    (
    )
)

1 个答案:

答案 0 :(得分: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;
}