public final class NodeSet
extends java.lang.Object
NodeSet
, it
could contain both "attributes" as well as "element" nodes as evaluated by
the expression. This result is returned as a long value which represents an
XPath ResultNode
. In short, a NodeSet
is a
collection of long values of type ResultNode
that represent
either attribute or element node. ResultNode
class contain
methods to work with these type of 'long
' values.
Document oDocument = new Document();
int iXml = oDocument.load("weather.xml");
XPath oXPath = XPath.getXPathInstance("//weather/forecast/node()");
NodeSet oNodeSet = oXPath.selectNodeSet(iXml);
//Iterate through the results
while (oNodeSet.hasNext())
{
long iResultNode= oNodeSet.next();//this is a handle to the XmlResultNode object in the native layer
if (ResultNode.isAttribute(iResultNode)){
String s = ResultNode.getStringValue(iResultNode);
} else {
int iNode = ResultNode.getElementNode(iResultNode);
}
}
Modifier | Constructor and Description |
---|---|
protected |
NodeSet(long iHandle)
This constructor will be used by the XPathResult class native method
|
Modifier and Type | Method and Description |
---|---|
void |
delete()
Deletes the native NodeSet handle.
|
protected void |
finalize() |
int[] |
getAttributeNodes()
This function returns the array of Element Nodes(NOM integer)
corresponding to attributes present in the Nodeset object.
|
java.lang.String[] |
getAttributeValues()
This function returns the array of strings which are the values of the
attributes present in the Nodeset object.
|
int[] |
getElementNodes()
This function returns the array of Element Nodes that the Nodeset object
contains.
|
long |
getNativeHandle()
This function returns the Native handle of this object.
|
boolean |
hasNext()
Returns false if the NodeSet is empty, true otherwise.
|
long |
next()
Returns a long value that represents an XPath ResultNode handle.
|
void |
show()
Displays the nodes present in the NodeSet.
|
int |
size()
Returns the number of
ResultNode s in the Nodeset. |
protected NodeSet(long iHandle)
iHandle
- The native handle to the Nodeset.public boolean hasNext()
<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()); NodeSet res = XPath.getMatchingNodeSet("//root/html4:table", meta, iXml); long iResultNode = 0; if (res.hasNext()) { iResultNode = res.next(); } assertEquals("table", ResultNode.getLocalName(iResultNode)); } catch (XMLException e) { e.printStackTrace(); } </code>
public int size()
ResultNode
s in the Nodeset.<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()); NodeSet res = XPath.getMatchingNodeSet("//root/html4:table", meta, iXml); assertEquals(1, res.size()); } catch (XMLException e) { e.printStackTrace(); } </code>
public long next()
<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()); NodeSet res = XPath.getMatchingNodeSet("//root/html4:table", meta, iXml); long iResultNode = 0; iResultNode = res.next(); assertEquals("table", ResultNode.getLocalName(iResultNode)); } catch (XMLException e) { e.printStackTrace(); } </code>
public void show()
<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table attr1=\"abc\">text</table><chair attr1=\"xyz\"/></root>" .getBytes()); NodeSet res = XPath.getMatchingNodeSet("//root/table", null, iXml); res.show();//displays number of items and element names in console. } 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()
<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table attr1=\"abc\">text</table><chair attr1=\"xyz\"/></root>" .getBytes()); NodeSet res = XPath.getMatchingNodeSet("//root/table", null, iXml); res.delete(); } catch (XMLException e) { e.printStackTrace(); } </code>
public int[] getElementNodes()
<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table attr1=\"abc\">text</table><chair attr1=\"xyz\"/></root>" .getBytes()); NodeSet res = XPath.getMatchingNodeSet("//root/table", null, iXml); int eleArr[] = res.getElementNodes(); assertEquals("table", Node.getName(eleArr[0])); } catch (XMLException e) { e.printStackTrace(); } </code>
public java.lang.String[] getAttributeValues()
<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table attr1=\"abc\">text</table><chair/></root>" .getBytes()); NodeSet res = XPath.getMatchingNodeSet("//root/table/@attr1", null, iXml); String StrArr[] = res.getAttributeValues(); assertEquals("abc", StrArr[0]); } catch (XMLException e) { e.printStackTrace(); } </code>
public int[] getAttributeNodes()
<code> Document oDocument = new Document(); int iXml; try { iXml = oDocument .load("<root><table attr1=\"abc\">text</table><chair/></root>" .getBytes()); NodeSet res = XPath.getMatchingNodeSet("//root/table/@attr1", null, iXml); int eleArr[] = res.getAttributeNodes(); assertEquals("table", Node.getName(eleArr[0])); } catch (XMLException e) { e.printStackTrace(); } </code>
public long getNativeHandle()