【问题标题】:How to extract data from a SipleXMLElement? [closed]如何从 SimpleXMLElement 中提取数据? [关闭]
【发布时间】:2012-02-16 17:53:44
【问题描述】:

我有一个包含以下内容的变量 $foo:

object(SimpleXMLElement)#969 (2) {
  ["@attributes"]=>
  array(1) {
    ["type"]=>
    string(4) "html"
  }
  [0]=>
  string(13) "Viernes Santo"
}

我正在尝试获取“Viernes Santo”的内容,但我不能……我尝试了$foo[0],但返回的内容相同……有什么想法吗?

【问题讨论】:

标签: php xml arrays xml-parsing


【解决方案1】:

SimpleXMLElement 的行为就像一个对象,这就是为什么人们会像你一样感到困惑,但它实际上是一种有点不同的语言结构。元素及其数据必须转换为您要使用的数据类型,因为默认情况下,从 SimpleXMLElement 函数返回的所有内容都是 SimpleXMLElement,如果不是由函数进行类型转换,则必须直接进行类型转换。

如果你打电话:

echo $foo; //typecast to a string automatically
$myVar = $foo; //SimpleXMLElement
$myVar = (string)$foo; //typecast to a string

【讨论】:

    【解决方案2】:

    这行得通:

    (string) $foo 
    

    【讨论】:

      猜你喜欢
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多