XMLUtils { public static void stripWhitespaceNodes(Node element) { Node node, child; for (child = element.getFirstChild(); child != null; child = node) { node = child.getNextSibling(); stripWhitespaceNodes(child); } if (element.getNodeType() == Node.TEXT_NODE && element.getNodeValue().trim().length()==0) { element.getParentNode().removeChild(element); } } private  XMLUtils(); static void initialize(XmlConfigurer xmlConfigurer); static LSParser createLSParser(); static LSSerializer createLSSerializer(); static LSInput createLSInput(); static LSOutput createLSOutput(); static Node findNodeByName(Document doc, String pathExpression); static void stripWhitespaceNodes(Node element); static String getNodesPathName(Node node); static String serialize(Document doc); static String prettyPrint(String xml); static Map<String, String> lookupNamespaces(Node referenceNode); static Document parseMessagePayload(String messagePayload); static Charset getTargetCharset(Document doc); static String omitXmlDeclaration(String xml); static boolean hasXmlPayload(Message message);  }
@Test public void testStripWhitespaceNodes() { Node testNode = Mockito.mock(Node.class); Node textNode = Mockito.mock(Node.class); Node whiteSpaceNode = Mockito.mock(Node.class); reset(testNode, textNode, whiteSpaceNode); when(testNode.getFirstChild()).thenReturn(whiteSpaceNode); when(testNode.getNodeType()).thenReturn(Document.ELEMENT_NODE); when(testNode.removeChild(whiteSpaceNode)).thenReturn(testNode); when(whiteSpaceNode.getNodeType()).thenReturn(Document.TEXT_NODE); when(whiteSpaceNode.getFirstChild()).thenReturn(null); when(whiteSpaceNode.getNextSibling()).thenReturn(textNode); when(whiteSpaceNode.getNodeValue()).thenReturn(""); when(whiteSpaceNode.getParentNode()).thenReturn(testNode); when(textNode.getNodeType()).thenReturn(Document.TEXT_NODE); when(textNode.getFirstChild()).thenReturn(null); when(textNode.getNextSibling()).thenReturn(null); when(textNode.getNodeValue()).thenReturn("This is a sample text"); XMLUtils.stripWhitespaceNodes(testNode); }