php xml get value of node

How can I get "bla" in the below dump of my node
Essentially bla is what is contained in my node
<mynode name="myvalueone" display="myvalue2">bla</mynode>

  public '@attributes' => 
      'name' => string 'myvalueone' (length=65)
      'display' => string 'myvalue2' (length=9)
  string 'bla' (length=3)

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The code below traverses the simplexml object and you can see the text gets printed out where there are no children of a node (therefore a leaf node) and the resulting string has a length greater than zero.

If you want to find the text for a specific node then add code to compare the $node->getName() to the one you want and then find the child node with no children.

Making alterations to this code should familiarize yourself with what is all in the returned simplexml object.

You can also run an xpath query which returns specific text from a input query.  Explained in the manual here:

for your example the command would look like:

$result = $sxml->xpath('/mynode');
foreach ($result as $oneresult) {
    echo "found", $oneresult, "<BR>";

      echo '<PRE>';
       $xml_file = "????.htmlorxml";
	$sxml = simplexml_load_file($xml_file,'SimpleXMLElement',LIBXML_NOCDATA);
	echo '</pre>';

	print dumpIt($sxml);
function dumpIt($node) {
	$output = "<DIV style=\"padding: 0px 0px 0px 20px;\">".$node->getName()."\n";
	$att = $node->attributes();
	if ($att) {  // is false if there are no attributes
		$output .= "<UL>";
		foreach ($att as $att_key => $att_value) {
			$output .= "<LI> [".$att_key ."] = ".$att_value;
		$output .= "</UL>";
	if ($node->count() == 0) { // check for leaf node
		$node_text = (string)$node;
		if (strlen($node_text) > 0) {
			$output .= "<UL>[internal text] = ". trim($node) ."</UL>\n";				
	} else {
		foreach ($node as $key => $value) {
			$output .= dumpIt($value);
	$output .= "</DIV><BR>\n";
	return $output;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NewtonianBAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.