All pastes #714028 Raw Edit

Something

public text v1 · immutable
#714028 ·published 2007-09-25 21:38 UTC
rendered paste body
	public Node getNodeById(Node node, String id) 
	{
		Node currentNode = node;
		

		while(currentNode != null) 
		{
			// Try hit-test
			if (hitTestNodeId(currentNode, id))
			{
				// Hit!
				return currentNode;
			}
			
			Node nextNode = currentNode.getFirstChild();
			if(nextNode != null) 
			{
				currentNode = nextNode;
				continue;
			}
			
			while(currentNode != null)
			{
				nextNode = currentNode.getNextSibling();
		        if (nextNode != null) 
		        {
		        	currentNode = nextNode;
		        	break;
		        }

		        if (currentNode != null)
		        {
		        	currentNode = currentNode.getParentNode();
		        	if (node == currentNode) 
		        	{
	    			   return null;
		        	}
		        }
		}
	}
	
	public boolean hitTestNodeId(Node node, String id)
	{
		if ( 	(node != null) && 
				(node.getAttributes() != null) &&
				(node.getAttributes().getNamedItem("id") != null) && 
				(node.getAttributes().getNamedItem("id").getNodeValue()).equals(id))
		{
			return true;
		}
		return false;
	}