我正在尝试访问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]的子级。
有人能帮我指引一下正确的方向吗?这样我就可以从title元素中获取文本了。
发布于 2012-04-20 02:22:30
对于你的例子,这对我很有效:
echo (string)$result[0];发布于 2012-04-20 02:22:38
在选择数组中的项后尝试强制转换为字符串:
[...]
$result = $xml->xpath('//h:title');
echo current($result);https://stackoverflow.com/questions/10233732
复制相似问题