DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Detect Spam Content in Documents Using C#
  • Responsible AI Is an Engineering Problem, not a Policy Document
  • Implementing Effective Document Fraud Detection in C#
  • Designing Docling Studio: Key Architecture Decisions

Trending

  • How SaaS Architectures Break at Scale — and the Engineering Decisions That Prevent It
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Using LLMs to Automate Data Cleaning and Transformation Pipelines
  • Build a GitHub Slack Bot With AWS Bedrock and MCP, Part 2

Remove Empty Nodes From Dom Document

By 
Egbert Minnaar user avatar
Egbert Minnaar
·
Jul. 27, 10 · Code Snippet
Likes (0)
Comment
Save
Tweet
Share
6.1K Views

Join the DZone community and get the full member experience.

Join For Free
// 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);
		}
	}
Document

Opinions expressed by DZone contributors are their own.

Related

  • How to Detect Spam Content in Documents Using C#
  • Responsible AI Is an Engineering Problem, not a Policy Document
  • Implementing Effective Document Fraud Detection in C#
  • Designing Docling Studio: Key Architecture Decisions

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook