php – 使用SimpleXMLElement 获取元素内部文本

参见英文答案 > Accessing certain properties of a SimpleXMLElement Object                                    1个
我试图访问xml元素的文本值.我正在使用SimpleXMLElement.我必须遗漏一些明显的东西.

<h:html xmlns:jr="http://openrosa.org/javarosa" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ex="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/2002/xforms">
  <h:head>
    <h:title>NewForm</h:title>      
  </h:head>
</h:html>

$xml = new SimpleXMLElement($resp);
$xml->registerXPathNamespace('h','http://www.w3.org/1999/xhtml'); 
// I have tried with and without the namespace (it doesn't seem to make a difference)

$result = $xml->xpath('//h:title');
debug($result);

运行上面的代码给了我:

array (
  0 => 
  SimpleXMLElement::__set_state(array(
     0 => 'NewForm',)),)

看起来很简单.我很难获得’NewForm’的价值

我试过了

$result [0],$result [0] – > {0},$result [0] [0].

迭代$result的子项[0].

有人可以帮助指导我正确的方向,以便我可以从标题元素获取文本?

解决方法

这对我的例子很有用:

echo (string)$result[0];

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...