New topics: Your Pet, IOU, Baby IQ, The Poisons, Birther II, Games, Future Power

Welcome to the Tech Space!

Wiki Articles

Skip to end of metadata
Go to start of metadata

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>

Labels:
java java Delete
random random Delete
programming programming Delete
xml xml Delete
dom dom Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.