Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Remove Empty Nodes From Dom Document

DZone's Guide to

Remove Empty Nodes From Dom Document

·
Free Resource
// Snippit to remove an empty node from the document.
// Node node contains the node (or whole DOM document) from which you want to remove.
// String nameToRemove is the name of the Node you want to remove.


	private static void removeEmptyNodes(Node node, String nameToRemove) {
		NodeList nodeList = node.getChildNodes();
		for(int i=0; i < nodeList.getLength(); i++){
			Node childNode = nodeList.item(i);
			String nodeName = childNode.getNodeName(); 
			if(nodeName.equals(nameToRemove) && childNode.getTextContent().equals("")){
				childNode.getParentNode().removeChild(childNode);
				i--;

			}
			removeEmptyNodes(childNode, nameToRemove);
		}
	}
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}