如何从SimpleXml中提取值?

时间:2016-08-31 15:58:19

标签: php xpath simplexml

如何从SimpleXml中提取值?我不断得到一个空阵,我不知道自己做错了什么。

我只想提取字符串"Familial GI Stromal Tumor With Loss of Heterozygosity and Amplification of Mutant KIT."

object(SimpleXMLElement)#13 (2) {
  ["@attributes"]=>
  array(2) {
    ["Name"]=>
    string(5) "Title"
    ["Type"]=>
    string(6) "String"
  }
  [0]=>
  string(86) "Familial GI Stromal Tumor With Loss of Heterozygosity and Amplification of Mutant KIT."
}

3 个答案:

答案 0 :(得分:0)

SimpleXMLElement__toString()方法。对于您在问题中显示的元素,您应该只能回显字符串内容。

echo $yourElement;

或者如果你想在变量中使用它,你可以明确地调用__toString()

$someVar = $yourElement->__toString();

或通过将元素视为字符串来触发它;

$someVar = "$yourElement";

答案 1 :(得分:0)

只需将其转换为字符串。

var_dump((string) $element);

无论如何,这取决于你的xpath。

答案 2 :(得分:0)

Thanks for the help, everyone. Turns out I figured it out. I simply had to access it from the array.

 $title = $pub->xpath('Item[@Name="Title"]')[0];