Retrieving Inner Text of Elements Using SimpleXMLElement

I am trying to access the text value of an xml element. I use SimpleXMLElement. I have to miss something obvious.

<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);

Executing the above code gives me:

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

It seems very simple. I find it difficult to get the value "NewForm"

I tried

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

Iterate through child elements $result[0].

Did someone help me in the right direction so that I can get the text from the title element?

+3
source share
2 answers

This worked for me with your example:

echo (string)$result[0];
+10
source

Try casting in a row after selecting an element in an array:

[...]
$result = $xml->xpath('//h:title');
echo current($result);
+1
source

All Articles