When a node contains a collection of subnodes of the same tag type, get a random node from them:
// General Utility Function
// Get random node of given tagname
Node getRandomNode(String sTagName,Node thisNode)
{
Node nResult = null;
Element eNode = (Element) thisNode;
NodeList childNodes = eNode.getElementsByTagName(sTagName);
if (childNodes.getLength()==0)
return null;
int randomIndex = rand.nextInt(childNodes.getLength());
nResult = childNodes.item(randomIndex);
return nResult;
}
This would work for this situation:
Node nRand = getRandomNode("link",weblinksNode);
<weblinks>
<link>http://www.google.com</link>
<link>http://www.bing.com</link>
<link>http://www.yahoo.com</link>
</weblinks>
Add Comment