SimpleXMLElement is used to represent an XML element. The typical usage is:
$resp = simplexml_load_file($httpcall);
$resp->somechild;
Note that somechild is the tag name of a child of the XML element that $resp represents.
If we want to extract the text within the tag, using $resp->somechild is usually enough because the SimpleXMLElement object $resp->somechild can be casted into a string when appropriate. But sometimes we need to use SimpleXMLElement ::__toString() to convert the SimpleXMLElement object to string explicitly, i.e., extract the text within the tag. For example, we can use if($resp->somechild==”12345″) to compare the string between the XML tags with “12345”, that is ok because $resp->somechild can be converted to string automatically. But we cannot use if($resp->somechild==$resp->anotherchild) to compare the text of two children of $resp. Even the text of the two children are the same, the result of the comparison is not equal since they are two different objects. In this case, we have to use SimpleXMLElement ::__toString() to convert them to strings before comparison, i.e. if($resp->somechild->__toString()==$resp->anotherchild->__toString())
Comments are closed, but trackbacks and pingbacks are open.