I fear that this is a really stupid question but I am really stuck after trying a load of combinations for the last 2 hours. I am trying to pull the NAME out of the XML file
My XML file:
<?xml version="1.0"?>
<userdata>
<name>John</name>
</userdata>
My php:
$doc = new DOMDocument();
$doc -> load( "thefile.xml" );
$thename = $doc -> getElementsByTagName( "name" );
$myname= $thename -> getElementsByTagName("name") -> item(0) -> nodeValue;
The Error:
Catchable fatal error: Object of class DOMElement could not be converted to string in phpreader.php
I have tried
$myname= $thename -> getElementsByTagName("name") -> item(0) ;
$myname= $doc -> getElementsByTagName("name") -> item(0) -> nodeValue;
$myname= $doc -> getElementsByTagName("name") -> item(0) ;
but all fail. Guess I have tried just about every combination except the correct one 🙁
You may want
$myname = $thename->item(0)->nodeValue. $thename is already the NodeList of all of the nodes whose tag is “name” – you want the first item of these (->item(0)), and you want the value of the node (->nodeValue).$thenameshould be more appropriately named$names, and you’d see why$names->item(0)->nodeValuemakes sense semantically.This Works For MeTM.