classdef XPath
properties
domFactory
builder
doc
factory
xpath
end
methods
function this = XPath( xmlFile )
import javax.xml.parsers.*;
import javax.xml.xpath.*;
this.domFactory = DocumentBuilderFactory.newInstance();
this.builder = this.domFactory.newDocumentBuilder();
this.doc = this.builder.parse(xmlFile);
this.factory = XPathFactory.newInstance();
this.xpath = this.factory.newXPath();
end
function result = evaluateExpression( this, expressionStr )
import javax.xml.xpath.*;
expression = this.xpath.compile(expressionStr);
result = expression.evaluate(this.doc, XPathConstants.NODESET);
end
function result = evaluateExpressionInElem( this, expressionStr, elem )
import javax.xml.xpath.*;
expression = this.xpath.compile(expressionStr);
result = expression.evaluate(elem, XPathConstants.NODESET);
end
function newDoc = setBaseDoc( this, expressionStr )
elem = evaluateExpression( this, expressionStr );
if ~isempty( elem ) && elem.getLength==1
this.doc = elem.item(0);
end
if nargout > 0
newDoc = this.doc;
end
end
end
end