Java tutorial
//package com.java2s; /* * JLib - Publicitas Java library. * * Copyright (c) 2005, 2006, 2007, 2008 Publicitas SA. * Licensed under LGPL (LGPL-LICENSE.txt) license. */ import org.w3c.dom.Node; public class Main { /** * Checks if a node has contents. * A node is considered as <i>having content</i> when: * <ul> * <li>When the node or any of its childs contains a non-empty attribute.</li> * <li>When the node or any of its childs contains non-empty text nodes. If the * parameter <b>ignoreBlankSpaces</b> is <i>true</i>, text nodes containing * only control characters (ascii codes equal or smaller than 32) are considered * empty.</li> * </ul> * Comment and processing instruction nodes are not considered as content. * @param x The node to check. If it is null, the method returns <i>false</i>. * @param ignoreBlankSpaces If <i>true</i> text nodes containing * only control characters (ascii codes equal or smaller than 32) are considered * empty. * @return <i>true</i> if the specified node has contents. */ static public boolean NodeHasContent(Node x, boolean ignoreBlankSpaces) { int n, nn; // To parse child nodes of "x". Node child; // One of the childs of "x". int childType; // Type of the child node. String childContent; // When the child is a text node, its content. //.............................. Looks for "non-empty" nodes ................... if (x != null) { nn = x.getChildNodes().getLength(); for (n = 0; n < nn; n++) { child = x.getChildNodes().item(n); childType = child.getNodeType(); switch (childType) { // Nodes containing text: case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: if (ignoreBlankSpaces) childContent = child.getNodeValue().trim(); else childContent = child.getNodeValue(); if (childContent.length() > 0) return true; // Elements (nodes containing nodes): case Node.ELEMENT_NODE: if (NodeHasContent(child, ignoreBlankSpaces)) return true; // Any other type of node are not considered as content: default: continue; } } } //............................. If no "non-empty" node has been found .......... return false; } }