public final class XPath
extends java.lang.Object
The XPath expression is evaluated with a NOM input. The result is returned as
either as a NodeSet
, boolean
, double
,
String
or as a generic XPathResult
object.
The usage methodology of XPath-NOM is as follows :
Assuming the integer iXml represents a nom node.
Document oDocument = new Document();
int iXml = oDocument.load("Book.xml");
XPath oXPath = new XPath("/book/chapter[1]/lesson[1]/text()");//The first lesson of the first chapter
//Return as a generic XPathResult.
XPathResult oXResult = oXPath.evaluate(iXml);
//To obtain the actual result from XPathResult object
int type = oXResult.getType();
switch( type )
{
case XPathResult.XPATH_BOOLEAN:
boolean bool= oXResult.getBooleanResult();
break;
case XPathResult.XPATH_NODESET:
NodeSet oNset= oXResult.removeNodeSetFromResult();
break;
case XPathResult.XPATH_NUMBER:
double dNum= oXResult.getNumberResult();
break;
case XPathResult.XPATH_STRING:
String st= oXResult.getStringResult();
break;
}
//In the above example, the return type is XPATH_NODESET.
If the XPath expression is evaluated as a NodeSet
, it could
contain both "attributes" as well as "element" nodes as evaluated by the
expression. This result is returned as a long value that is of type
ResultNode.
In short, a NodeSet
is a collection of
long values that are of type ResultNode
that represent either
attribute or element node. ResultNode
class contains methods to
work with these type of integers.
XPath oXPath = new XPath("SOME_XPATH_EXPRESSION");
//The user, if he knows the type of result
//that will be returned for the given expression,
//he can use the following:
// 1. return result as a number ( a floating point number )
double dVal = oXPath.evaluateNumberResult(iXml);
// 2. return result as a string
String str= oXPath.evaluateStringResult(iXml);
// 3. return result as a boolean
boolean bVal = oXPath.evaluateBooleanResult(iXml);
// 4. return result as a Nodeset ( this contains attributes as well as elements)
NodeSet oNodeSet= oXPath.selectNodeSet(iXml);
Root Node Handling :
XPath perceives a separate root node apart from the document node.i.e.,
The outer most element is the child of the root node.
eg.,
<first>
In the above xml, the root node is NOT the "first" node.
Instead, XPath perceives an imaginary root of which the element "first" is
the child.
<second/>
<third/>
</first>
<Imaginary_root>
<first>
<second/>
<third/>
</first>
</Imaginary_root>
For NOM model, the document element (outermost element)is the root. XPath-NOM
implementation assumes an imaginary root to adhere to the XPath
Specification.
Note :
If the input xml is passed as zero, the result will
be a null value.
If the there was no matching elements available for
the given expression, then the result will be an empty result.
If the input arguments are null or invalid, then the
corresponding exception will be thrown.
Constructor and Description |
---|
XPath(java.lang.String sExpression)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
void |
delete() |
XPathResult |
evaluate(int iNode)
Evaluates the Xpath expression to the most generic result form
XPathResult taking the NOM integer as input. |
XPathResult |
evaluate(int iNode,
XPathMetaInfo oMeta)
Evaluates the Xpath expression to the most generic result form
XPathResult . |
XPathResult |
evaluate(long iResultNode)
Evaluates the Xpath expression to the most generic result form
XPathResult taking ResultNode handle as the input. |
XPathResult |
evaluate(long iResultNode,
XPathMetaInfo oMeta)
Evaluates the Xpath expression to the most generic result form
XPathResult . |
XPathResult |
evaluate(XPathMetaInfo oMeta)
Evaluates the XPath expression to the most generic result form
XPathResult taking MetaInfo as input. |
boolean |
evaluateBooleanResult(int iNode)
Evaluates the Xpath expression whose result is a Boolean value.
|
boolean |
evaluateBooleanResult(int iNode,
XPathMetaInfo oMetaInfo)
Evaluates the Xpath expression whose result is a Boolean value.
|
static XPathResult |
evaluateExpression(java.lang.String exp,
XPathMetaInfo metaInfo,
int node)
It evaluates the XPath expression and returns the XPathResult object.
|
double |
evaluateNumberResult(int iNode)
Evaluates the Xpath expression whose result is a Number.
|
double |
evaluateNumberResult(int iNode,
XPathMetaInfo oMetaInfo)
Evaluates the Xpath expression whose result is a Number.
|
java.lang.String |
evaluateStringResult(int iNode)
Evaluates the Xpath expression whose result is a
String . |
java.lang.String |
evaluateStringResult(int iNode,
XPathMetaInfo oMetaInfo)
Evaluates the Xpath expression whose result is a
String . |
XPathResult |
evaluateWithContext(int node,
javax.xml.xpath.XPath customContext)
Evaluate the expression based on the context specified.
|
protected void |
finalize() |
int |
firstMatch(int iNode,
XPathMetaInfo metaInfo) |
long |
firstMatchWithCustomContext(int iNode,
javax.xml.xpath.XPath customContext)
Select the first node which matches the give pattern.
|
static int |
getFirstMatch(java.lang.String exp,
XPathMetaInfo metaInfo,
int node)
It returns the first node which matches the pattern.
|
java.util.List<java.lang.String> |
getLocationSteps() |
java.util.Vector |
getLocationStepsInExp()
Deprecated.
|
static int[] |
getMatchingNodes(java.lang.String exp,
XPathMetaInfo metaInfo,
int node)
It returns Array of node which matches the pattern.
|
static NodeSet |
getMatchingNodeSet(java.lang.String exp,
XPathMetaInfo metaInfo,
int node)
It returns the matching NodeSet.
|
protected long |
getNativeHandle() |
static int |
getNumXPathExpressionObjects()
Returns the total number of XPath Expression objects created
|
static int |
getNumXPathObjects()
Returns the total number of XPath objects created
|
java.lang.String |
getSourceExpression()
It returns the XPath Expression for the given XPath object.
|
static int |
getTotalXPathExpressionObjects()
Returns the total number of XPath expression objects created
|
static int |
getTotalXPathObjects()
Returns the total number of XPath objects created
|
static XPath |
getXPathInstance(java.lang.String sExpression)
Factory method to create XPath object
|
java.lang.String[] |
selectAttributeValues(int iNode) |
java.lang.String[] |
selectAttributeValues(int iNode,
XPathMetaInfo metaInfo) |
int[] |
selectElementNodes(int iNode) |
int[] |
selectElementNodes(int iNode,
XPathMetaInfo metaInfo) |
NodeSet |
selectNodeSet(int iNode)
Evaluates the Xpath expression taking NOM integer as input
|
NodeSet |
selectNodeSet(int iNode,
XPathMetaInfo oMetaInfo)
Evaluates the Xpath expression taking XPathMetaInfo and NOM integer as
input and returns result as a NodeSet.
|
NodeSet |
selectNodeSet(long iResultNode)
Evaluates the Xpath expression taking ResultNode handle as input
|
NodeSet |
selectNodeSet(long iResultNode,
XPathMetaInfo oMetaInfo)
Evaluates the Xpath expression taking XPathMetaInfo and
ResultNode handle as input and returns result as a NodeSet. |
public XPath(java.lang.String sExpression)
sExpression
- The Xpath expression that is to be evaluated.public static XPath getXPathInstance(java.lang.String sExpression)
sExpression
- The Xpath expression that is to be evaluated. <code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table>text</table><chair/><table>abc</table></root>" .getBytes()); XPath oXPath0 = XPath.getXPathInstance("string(//root/table)"); XPathResult res = oXPath0.evaluate(iXml); assertEquals(XPathResult.XPATH_STRING, res.getType()); } catch (XMLException e) { e.printStackTrace(); } </code>
public static int getNumXPathObjects()
<code> int res = XPath.getNumXPathObjects(); </code>
public static int getNumXPathExpressionObjects()
<code> int res = XPath.getNumXPathExpressionObjects(); </code>
public double evaluateNumberResult(int iNode)
iNode
- The NOM Handle to the input XMLXPathResult.XPATH_NUMBER
.
String xml = "<root><table attr='5'>1</table><chair/></root>";
Document oDocument = new Document();
int iXml;
try {
iXml = oDocument.load(xml.getBytes());
XPath oXPath = XPath.getXPathInstance("count(//root/table/@attr)");
assertEquals(5, oXPath.evaluateNumberResult(iXml));
} catch (XMLException e) {
e.printStackTrace();
}
public double evaluateNumberResult(int iNode, XPathMetaInfo oMetaInfo)
iNode
- The NOM Handle to the input XMLXPathResult.XPATH_NUMBER
.
String xml = "<root><table attr='5'>text</table><chair/></root>";
Document oDocument = new Document();
XPathMetaInfo meta = new XPathMetaInfo();
meta.addNamespaceBinding("ns","http://www.xyz.com");
int iXml;
try {
iXml = oDocument.load(xml.getBytes());
XPath oXPath = XPath.getXPathInstance("count(//ns:root/ns:table/@attr)");
assertEquals(5, oXPath.evaluateNumberResult(iXml, meta));
} catch (XMLException e) {
e.printStackTrace();
}
public boolean evaluateBooleanResult(int iNode)
iNode
- The NOM Handle to the input XMLXPathResult.XPATH_BOOLEAN
.
String xml = "<root><table attr='5'>text</table><chair/></root>";
Document oDocument = new Document();
int iXml;
try {
iXml = oDocument.load(xml.getBytes());
XPath oXPath = XPath.getXPathInstance("//root/table/@attr!=4");
assertEquals(true, oXPath.evaluateBooleanResult(iXml));
} catch (XMLException e) {
e.printStackTrace();
}
public boolean evaluateBooleanResult(int iNode, XPathMetaInfo oMetaInfo)
iNode
- The NOM Handle to the input XMLoMetaInfo
- The XPathMetaInfo objectXPathResult.XPATH_BOOLEAN
.
String xml = "<root xmlns='http://www.xyz.com'><table attr='5'>text</table><chair/></root>
Document oDocument = new Document();
XPathMetaInfo meta = new XPathMetaInfo();
meta.addNamespaceBinding("ns","http://www.xyz.com");
int iXml;
try {
iXml = oDocument.load(xml.getBytes());
XPath oXPath = XPath.getXPathInstance("//ns:root/ns:table/@attr!=4");
assertEquals(true, oXPath.evaluateBooleanResult(iXml, meta));
} catch (XMLException e) {
e.printStackTrace();
}
public java.lang.String evaluateStringResult(int iNode)
String
. iNode
- The NOM Handle to the input XMLXPathResult.XPATH_STRING
.
String xml = "<root><table>text</table><chair/></root>";
Document oDocument = new Document();
int iXml;
try {
iXml = oDocument
.load(xml.getBytes());
XPath oXPath = XPath.getXPathInstance("string(//root/table)");
assertEquals("text", oXPath.evaluateStringResult(iXml));
} catch (XMLException e) {
e.printStackTrace();
}
public java.lang.String evaluateStringResult(int iNode, XPathMetaInfo oMetaInfo)
String
. iNode
- The NOM Handle to the input XMLXPathResult.XPATH_STRING
.
String xml = "<root xmlns='http://www.xyz.com'><table>text</table><chair/></root>";
Document oDocument = new Document();
XPathMetaInfo meta = new XPathMetaInfo();
meta.addNamespaceBinding("ns","http://www.xyz.com");
int iXml;
try {
iXml = oDocument
.load(xml.getBytes());
XPath oXPath = XPath.getXPathInstance("string(//ns:root/ns:table)");
assertEquals("text", oXPath.evaluateStringResult(iXml, meta));
} catch (XMLException e) {
e.printStackTrace();
}
public NodeSet selectNodeSet(int iNode, XPathMetaInfo oMetaInfo)
iNode
- The NOM integer handle.oMetaInfo
- The MetaInfo object<code> Document oDocument = new Document(); int iXml; try { XPathMetaInfo meta = new XPathMetaInfo(); meta.addNamespaceBinding("html4", "http://www.w3.org/TR/html4"); iXml = oDocument .load("<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table attr1=\"abc\">text</h:table><chair/></root>" .getBytes()); XPath oXPath0 = XPath.getXPathInstance("//root/html4:table"); NodeSet oNodeset = oXPath0.selectNodeSet(iXml, meta); assertEquals(1, oNodeset.size()); } catch (XMLException e) { e.printStackTrace(); } </code>
public NodeSet selectNodeSet(long iResultNode, XPathMetaInfo oMetaInfo)
ResultNode
handle as input and returns result as a NodeSet.iResultNode
- The Result Node handle.oMetaInfo
- The MetaInfo object<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><child1><gchild1><ggchild1/></gchild1></child1></root>" .getBytes()); XPath oXPath0 = XPath.getXPathInstance("//child1"); NodeSet oNodeset = oXPath0.selectNodeSet(iXml, null); long iResultNode = oNodeset.next(); oXPath0 = XPath.getXPathInstance("//ggchild1"); oNodeset = oXPath0.selectNodeSet(iResultNode, null); iResultNode = oNodeset.next(); assertEquals("ggchild1", ResultNode.getName(iResultNode)); } catch (XMLException e) { e.printStackTrace(); } </code>
public NodeSet selectNodeSet(int iNode)
type
- The NOM integer<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table>text</table><chair/></root>".getBytes()); XPath oXPath0 = XPath.getXPathInstance("//root/table"); NodeSet oNodeset = oXPath0.selectNodeSet(iXml); assertEquals(1, oNodeset.size()); } catch (XMLException e) { e.printStackTrace(); } </code>
public NodeSet selectNodeSet(long iResultNode)
iResultNode
- The ResultNode handle<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table>text<chair/></table></root>".getBytes()); XPath oXPath0 = XPath.getXPathInstance("//root/table"); NodeSet oNodeset = oXPath0.selectNodeSet(iXml); long iResNode = oNodeset.next(); oXPath0 = XPath.getXPathInstance("//table/chair"); oNodeset = oXPath0.selectNodeSet(iResNode); assertEquals(1, oNodeset.size()); } catch (XMLException e) { e.printStackTrace(); } </code>
public XPathResult evaluate(int iNode)
XPathResult
taking the NOM integer as input.iNode
- The NOM integer handle<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table>text</table><chair/><table>abc</table></root>" .getBytes()); XPath oXPath0 = XPath.getXPathInstance("string(//root/table)"); XPathResult res = oXPath0.evaluate(iXml); assertEquals(XPathResult.XPATH_STRING, res.getType()); } catch (XMLException e) { e.printStackTrace(); } </code>
public XPathResult evaluate(long iResultNode)
XPathResult
taking ResultNode handle as the input.iNode
- The ResultNode.<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table>text<chair/></table></root>".getBytes()); XPath oXPath0 = XPath.getXPathInstance("//root/table"); NodeSet oNodeset = oXPath0.selectNodeSet(iXml); long iResNode = oNodeset.next(); oXPath0 = XPath.getXPathInstance("string(//table/chair)"); XPathResult res = oXPath0.evaluate(iResNode); assertEquals(XPathResult.XPATH_STRING, res.getType()); } catch (XMLException e) { e.printStackTrace(); } </code>
public XPathResult evaluate(int iNode, XPathMetaInfo oMeta)
XPathResult
.iNode
- The NOM integeroMeta
- XPathMetaInfo object.<code> Document oDocument = new Document(); int iXml; try { XPathMetaInfo meta = new XPathMetaInfo(); meta.addNamespaceBinding("html4", "http://www.w3.org/TR/html4"); iXml = oDocument .load("<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table attr1=\"abc\">text</h:table><chair/></root>" .getBytes()); XPath oXPath0 = XPath.getXPathInstance("string(//root/html4:table)"); XPathResult res = oXPath0.evaluate(iXml,meta); assertEquals(XPathResult.XPATH_STRING, res.getType()); } catch (XMLException e) { e.printStackTrace(); } </code>
public XPathResult evaluate(long iResultNode, XPathMetaInfo oMeta)
XPathResult
.iResultNode
- The ResultNodeoMeta
- XPathMetaInfo object.<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table>text</h:table><chair/></root>" .getBytes()); XPathMetaInfo meta = new XPathMetaInfo(); meta.addNamespaceBinding("html4", "http://www.w3.org/TR/html4"); XPath oXPath0 = XPath.getXPathInstance("//html4:table"); NodeSet oNodeset = oXPath0.selectNodeSet(iXml, meta); Long iResultNode = oNodeset.next(); XPathResult oResult = oXPath0.evaluate(iResultNode, meta); System.out.println(oResult.getType()); assertTrue(oResult.getType() == XPathResult.XPATH_NODESET); } catch (XMLException e) { e.printStackTrace(); } </code>
public XPathResult evaluate(XPathMetaInfo oMeta)
XPathResult
taking MetaInfo as input. oMeta
- MetaInfo Object.null
if MetaInfo is
null
<code> XPathMetaInfo oMeta = new XPathMetaInfo(); XPathResult xRes = XPathResult.getInstance(90); XPath oXPath = XPath.getXPathInstance("$var + $var"); oMeta.addVarBinding("var", xRes); XPathResult xresult = oXPath.evaluate(oMeta); </code>
public XPathResult evaluateWithContext(int node, javax.xml.xpath.XPath customContext)
node
- - The NOM node to be searched up on.customContext
- - The context to find the namespace, variable and function
bindings.public int[] selectElementNodes(int iNode)
iNode
- The NOM Handle to the input XML<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table>text</table><chair/></root>".getBytes()); XPath oXPath0 = XPath.getXPathInstance("//root/*"); int res[] = oXPath0.selectElementNodes(iXml); assertEquals("chair", Node.getName(res[1])); } catch (XMLException e) { e.printStackTrace(); } </code>
public java.lang.String[] selectAttributeValues(int iNode)
iNode
- The NOM Handle to the input XML<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table attr1=\"abc\">text</table><chair attr1=\"xyz\"/></root>" .getBytes()); XPath oXPath0 = XPath.getXPathInstance("//@attr1"); String strArr[] = oXPath0.selectAttributeValues(iXml); assertEquals("abc", strArr[0]); assertEquals("xyz", strArr[1]); } catch (XMLException e) { e.printStackTrace(); } </code>
public int[] selectElementNodes(int iNode, XPathMetaInfo metaInfo)
iNode
- The NOM Handle to the input XMLmetaInfo
- MetaInfo to be passed for namespace or attribute bindings<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table>text</h:table><chair/></root>" .getBytes()); XPathMetaInfo meta = new XPathMetaInfo(); meta.addNamespaceBinding("html4", "http://www.w3.org/TR/html4"); XPath oXPath0 = XPath.getXPathInstance("//root/html4:table"); int res[] = oXPath0.selectElementNodes(iXml, meta); assertEquals("h:table", Node.getName(res[0])); } catch (XMLException e) { e.printStackTrace(); } </code>
public java.lang.String[] selectAttributeValues(int iNode, XPathMetaInfo metaInfo)
iNode
- The NOM Handle to the input XMLmetaInfo
- MetaInfo to be passed for namespace or attribute bindings<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table attr1=\"abc\">text</h:table><chair/></root>" .getBytes()); XPathMetaInfo meta = new XPathMetaInfo(); meta.addNamespaceBinding("html4", "http://www.w3.org/TR/html4"); XPath oXPath0 = XPath.getXPathInstance("//root/html4:table/@attr1"); String strArr[] = oXPath0.selectAttributeValues(iXml, meta); assertEquals("abc", strArr[0]); } catch (XMLException e) { e.printStackTrace(); } </code>
public int firstMatch(int iNode, XPathMetaInfo metaInfo)
iNode
- The NOM Handle to the input XMLmetaInfo
- MetaInfo to be passed for namespace or attribute bindings<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table attr1=\"abc\">text</h:table><chair/></root>" .getBytes()); XPathMetaInfo meta = new XPathMetaInfo(); meta.addNamespaceBinding("html4", "http://www.w3.org/TR/html4"); XPath oXPath0 = XPath.getXPathInstance("//root/html4:table/@attr1"); int res = oXPath0.firstMatch(iXml, meta); assertEquals("h:table", Node.getName(res)); } catch (XMLException e) { e.printStackTrace(); } </code>
public long firstMatchWithCustomContext(int iNode, javax.xml.xpath.XPath customContext)
iNode
- - The NOM nodecustomContext
- - The Custom context@Deprecated public java.util.Vector getLocationStepsInExp()
<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table>text</table><chair/><table>abc</table></root>" .getBytes()); XPath oXPath0 = XPath.getXPathInstance("//root/table or //root/furniture"); Vector res = oXPath0.getLocationStepsInExp(); assertEquals(2, res.size()); assertEquals("/descendant-or-self::node( )/child::root/child::table", res.get(0).toString()); assertEquals("/descendant-or-self::node( )/child::root/child::furniture", res.get(1).toString()); } catch (XMLException e) { e.printStackTrace(); } </code>
public java.util.List<java.lang.String> getLocationSteps()
<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table>text</table><chair/><table>abc</table></root>" .getBytes()); XPath oXPath0 = XPath.getXPathInstance("//root/table or //root/furniture"); List<String> res = oXPath0.getLocationSteps(); assertEquals(2, res.size()); assertEquals("/descendant-or-self::node( )/child::root/child::table", res.get(0)); assertEquals("/descendant-or-self::node( )/child::root/child::furniture", res.get(1)); } catch (XMLException e) { e.printStackTrace(); } </code>
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public void delete()
public static int getFirstMatch(java.lang.String exp, XPathMetaInfo metaInfo, int node)
exp
- XPath expression to be evaluatedmetaInfo
- MetaInfo to be passed for namespace or attribute bindingsnode
- The NOM Handle to the input XML<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table attr1=\"abc\">text</h:table><chair/></root>" .getBytes()); XPathMetaInfo meta = new XPathMetaInfo(); meta.addNamespaceBinding("html4", "http://www.w3.org/TR/html4"); int res = XPath.getFirstMatch("//root/html4:table/@attr1", meta, iXml); assertEquals("h:table", Node.getName(res)); } catch (XMLException e) { e.printStackTrace(); } </code>
public static int[] getMatchingNodes(java.lang.String exp, XPathMetaInfo metaInfo, int node)
exp
- XPath expression to be evaluatedmetaInfo
- MetaInfo to be passed for namespace or attribute bindingsnode
- The NOM Handle to the input XML<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table attr1=\"abc\">text</h:table><chair/></root>" .getBytes()); XPathMetaInfo meta = new XPathMetaInfo(); meta.addNamespaceBinding("html4", "http://www.w3.org/TR/html4"); int res[] = XPath.getMatchingNodes("//root/html4:table", meta, iXml); assertEquals("h:table", Node.getName(res[0])); } catch (XMLException e) { e.printStackTrace(); } </code>
public static NodeSet getMatchingNodeSet(java.lang.String exp, XPathMetaInfo metaInfo, int node)
exp
- XPath expression to be evaluatedmetaInfo
- MetaInfo to be passed for namespace or attribute bindingsnode
- The NOM Handle to the input XML<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table attr1=\"abc\">text</h:table><chair/></root>" .getBytes()); XPathMetaInfo meta = new XPathMetaInfo(); meta.addNamespaceBinding("html4", "http://www.w3.org/TR/html4"); NodeSet res = XPath.getMatchingNodeSet("//root/html4:table", meta, iXml); long iResultNode = res.next(); assertEquals("h:table", ResultNode.getName(iResultNode)); } catch (XMLException e) { e.printStackTrace(); } </code>
public static XPathResult evaluateExpression(java.lang.String exp, XPathMetaInfo metaInfo, int node)
exp
- XPath expression to be evaluatedmetaInfo
- MetaInfo to be passed for namespace or attribute bindingsnode
- The NOM Handle to the input XML<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root xmlns:h=\"http://www.w3.org/TR/html4\" ><h:table attr1=\"abc\">text</h:table><chair/></root>" .getBytes()); XPathMetaInfo meta = new XPathMetaInfo(); meta.addNamespaceBinding("html4", "http://www.w3.org/TR/html4"); XPathResult res = XPath .evaluateExpression("//root/html4:table", meta, iXml); assertEquals(XPathResult.XPATH_NODESET, res.getType()); } catch (XMLException e) { e.printStackTrace(); } </code>
public java.lang.String getSourceExpression()
<code> XPath oXPath0 = XPath.getXPathInstance("//root/html4:table"); assertEquals("//root/html4:table", oXPath0.getSourceExpression()); </code>
public static int getTotalXPathObjects()
<code> Path.getTotalXPathObjects(); </code>
public static int getTotalXPathExpressionObjects()
<code> Path.getTotalXPathExpressionObjects(); </code>
protected long getNativeHandle()