im trying to filter out some informations from an XML file using PHP xpath, somehow the response seems always to be empty. Does anyone have an idea what could cause this?
Xml example:
<account>
<a>Information1</a>
<b>Information2</b>
<c>Informatio3</c>
</account>
My actual code:
public static function user_cache($username) {
$xml = @file_get_contents('xmlfile');
$xml = @simplexml_load_string($xml);
if ( $xml ) {
$results = array(
"userid" => $xml->xpath('a')
);
}
else {
$results['message'] = 'Unable to get data.';
}
return $results;
}
{
"userid": []
}
This is more of a XPATH question. The correct syntax would be
$xml->xpath('//a')
You can find more information about XPATH's syntax here: http://www.w3schools.com/xsl/xpath_syntax.asp
On the other hand, xpath method returns an array of SimpleXMLElement, so take that into account. http://php.net/manual/en/simplexmlelement.xpath.php