Code example for Node

Methods: appendChildgetParentNodeinsertBeforeremoveChild

0
	 *            inserted. 
	 */ 
	public static void insertBefore(Node newNode,Node refNode)
	{ 
 
		Node parentNode=refNode.getParentNode();
 
		if(parentNode==null)
		{ 
			System.out.println("Cannot insert ["+newNode+"] before ["+refNode
					+"]. ["+refNode+"] has no parent.");
			return; 
		} 
 
		if(parentNode instanceof Document
				&&newNode.getNodeType()==Node.ELEMENT_NODE)
		{ 
			System.out
					.println("Request to insert an element before the Document root node.  This is not allowed.  Replacing the Document root with the new Node.");
			parentNode.removeChild(refNode);
			parentNode.appendChild(newNode);
		} 
		else 
		{ 
			parentNode.insertBefore(newNode,refNode);
		} 
	} 
 
	/** 
	 * Insert the supplied nodes before the supplied reference node (refNode). 
Experience pair programming with AI  Get Codota for Java