protected void sortChildNodes( Node node, boolean descending, int depth, Comparator comparator ) {
		NodeList childNodeList = node.getChildNodes();
		List nodes = new ArrayList();
		if( depth > 0 && childNodeList.getLength() > 0 ) {
			for( int i = 0; i < childNodeList.getLength(); i++ ) {
				Node tNode = childNodeList.item( i );
				sortChildNodes( tNode, descending, depth - 1, comparator );
				// Remove empty text nodes
				if( ( !( tNode instanceof Text ) ) ||
						( tNode instanceof Text && ( ( Text ) tNode ).getTextContent().trim().length() > 1 ) ) {
					nodes.add( tNode );
				}
			}
			Comparator comp = ( comparator != null ) ? comparator : new DefaultNodeNameComparator();
			if( descending ) {
				//if descending is true, get the reverse ordered comparator
				Collections.sort( nodes, Collections.reverseOrder( comp ) );
			}
			else {
				Collections.sort( nodes, comp );
			}

			for( Iterator iter = nodes.iterator(); iter.hasNext(); ) {
				Node element = ( Node ) iter.next();
				node.appendChild( element );
			}
		}
	}