/** * This method removes all <code>Text</code> nodes which are made up of only * white space. * * @param n the node which is to have all of its whitespace descendents * removed. */ public static void removeWhitespace(Node n) { removeWhitespaceInner(n, null); }
private static void removeWhitespaceInner(Node n, Node parent) { // This n is removed from the parent if n is a whitespace node if (parent != null && n instanceof Text && (!(n instanceof CDATASection))) { Text t = (Text) n; if (t.getData().matches("[ \t\n]*")) { parent.removeChild(t); } } if (n.hasChildNodes()) { int length = n.getChildNodes().getLength(); List<Node> toBeProcessed = new ArrayList<Node>(); // We collect all the nodes to iterate as the child nodes will change // upon removal for (int i = 0; i < length; i++) { toBeProcessed.add(n.getChildNodes().item(i)); } // This changes the child nodes, but the iterator of nodes never changes // meaning that this is safe for (Node childNode : toBeProcessed) { removeWhitespaceInner(childNode, n); } } }
/** * This method removes all <code>Text</code> nodes which are made up of only * white space. * * @param n the node which is to have all of its whitespace descendents * removed. */ public static void removeWhitespace(Node n) { removeWhitespaceInner(n, null); }
/** * This method removes all <code>Text</code> nodes which are made up of only * white space. * * @param n the node which is to have all of its whitespace descendents * removed. */ public static void removeWhitespace(Node n) { removeWhitespaceInner(n, null); }
private static void removeWhitespaceInner(Node n, Node parent) { // This n is removed from the parent if n is a whitespace node if (parent != null && n instanceof Text && (!(n instanceof CDATASection))) { Text t = (Text) n; if (t.getData().matches("[ \t\n]*")) { parent.removeChild(t); } } if (n.hasChildNodes()) { int length = n.getChildNodes().getLength(); List<Node> toBeProcessed = new ArrayList<Node>(); // We collect all the nodes to iterate as the child nodes will change // upon removal for (int i = 0; i < length; i++) { toBeProcessed.add(n.getChildNodes().item(i)); } // This changes the child nodes, but the iterator of nodes never changes // meaning that this is safe for (Node childNode : toBeProcessed) { removeWhitespaceInner(childNode, n); } } }
private static void removeWhitespaceInner(Node n, Node parent) { // This n is removed from the parent if n is a whitespace node if (parent != null && n instanceof Text && (!(n instanceof CDATASection))) { Text t = (Text) n; if (t.getData().matches("[ \t\n]*")) { parent.removeChild(t); } } if (n.hasChildNodes()) { int length = n.getChildNodes().getLength(); List<Node> toBeProcessed = new ArrayList<Node>(); // We collect all the nodes to iterate as the child nodes will change // upon removal for (int i = 0; i < length; i++) { toBeProcessed.add(n.getChildNodes().item(i)); } // This changes the child nodes, but the iterator of nodes never changes // meaning that this is safe for (Node childNode : toBeProcessed) { removeWhitespaceInner(childNode, n); } } }