Tuesday, 17 February 2015

Why do I get "Class 'DOMXPath' not found" exception?



I'm writing some code for parsing and editing an XML file.


I've used the code before and everything seemed fine, but I'm using it in another project now so have introduced namespaces.


In the following code, I keep getting the Class 'DOMXPath' not found error and can't work out why:



namespace Vendor\Project\Editor\Lib

class xmlEditor {
function __construct($xmlpath)
{
$this->loadXML($xmlpath);
}

public function loadXML($path)
{
$this->xmlDOM = new \DOMDocument();
$this->xmlDOM->load($path);
$this->path = realpath($path);
}

public function getNodeByPath($query)
{
$xpath = new \DOXXPath($this->xmlDOM);
$node = $xpath->query($query);

return $node->item(0);
}
}


And the code to get this running:



namespace Vendor\Project\Editor\Lib;

class triggers {
private static function getXML( $xmlPath )
{
return new xmlEditorAPI( $xmlPath );
}

public static function getNode($xPath)
{
$xmlObj = self::getXML();

$path = $xmlObj->getNodeByPath($xPath);
print (json_encode ($xmlObj->getDOMNode($path)));
}
}


It's $xpath = new \DOXXPath($this->xmlDOM); that triggers the error. I'm explicitly setting the path so I'm not sure why there's a problem.


No comments:

Post a Comment