Java Utililty Methods XML Node Remove

List of utility methods to do XML Node Remove

Description

The list of methods to do XML Node Remove are organized into topic(s).

Method

voidremoveEmptyLines(Node node)
Remove empty lines which are a descendant of node
Set<Node> nodesToRemove = new HashSet<Node>();
collectEmptyLines(node, nodesToRemove);
for (Node n : nodesToRemove) {
    n.getParentNode().removeChild(n);
voidremoveEmptyNodes(Node node)
remove Empty Nodes
NodeList list = node.getChildNodes();
List<Node> nodesToRecursivelyCall = new LinkedList<Node>();
for (int i = 0; i < list.getLength(); i++) {
    nodesToRecursivelyCall.add(list.item(i));
for (Node tempNode : nodesToRecursivelyCall) {
    removeEmptyNodes(tempNode);
boolean emptyElement = node.getNodeType() == Node.ELEMENT_NODE && node.getChildNodes().getLength() == 0;
boolean emptyText = node.getNodeType() == Node.TEXT_NODE && node.getNodeValue().trim().isEmpty();
if (emptyElement || emptyText) {
    node.getParentNode().removeChild(node);
voidremoveEmptyParagraphs(Node root)
remove Empty Paragraphs
NodeList pNodes = ((Element) root).getElementsByTagName("text:p");
for (int i = 0; i < pNodes.getLength(); i++) {
    Node node = pNodes.item(i);
    if (node.getTextContent().trim().equals("")) {
        if (!node.hasChildNodes()) {
            node.getParentNode().removeChild(node);
            i--;
        } else {
...
voidremoveEmptyTextNodes(Node parentNode)
remove Empty Text Nodes
Node childNode = parentNode.getFirstChild();
while (childNode != null) {
    Node nextChild = childNode.getNextSibling();
    short nodeType = childNode.getNodeType();
    if (nodeType == Node.TEXT_NODE) {
        boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty();
        if (containsOnlyWhitespace) {
            parentNode.removeChild(childNode);
...
voidremoveEmptyTextNodes(Node parentNode)
Removes all empty or whitespace only text nodes from the given parent node.
Node childNode = parentNode.getFirstChild();
while (childNode != null) {
    Node nextChild = childNode.getNextSibling();
    short nodeType = childNode.getNodeType();
    if (nodeType == Node.TEXT_NODE) {
        boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty();
        if (containsOnlyWhitespace) {
            parentNode.removeChild(childNode);
...
NoderemoveJavascripts(Node node)
Recursive method that traverses text node and removes any markup that could possibly contain javascript code.
if (node == null)
    return node;
if ("script".equalsIgnoreCase(node.getNodeName())) {
    node.getParentNode().removeChild(node);
NamedNodeMap attributes = node.getAttributes();
if (attributes != null) {
    for (int attrIdx = 0; attrIdx < attributes.getLength(); attrIdx++) {
...
voidremoveLeafNode(Node node)
Removes the Leaf Node
if (!node.hasChildNodes())
    node.getParentNode().removeChild(node);
NoderemoveNamedItem(final NamedNodeMap nodeMap, final String name)
remove Named Item
return nodeMap == null ? null : nodeMap.removeNamedItem(name);
voidremoveNamespaceDeclarations(Node node, String... namespaces)
remove Namespace Declarations
NamedNodeMap namedNodeMap = ((Element) node).getAttributes();
for (int nameIndex = 0; nameIndex < namedNodeMap.getLength(); nameIndex++) {
    Node namedNode = namedNodeMap.item(nameIndex);
    String uri = namedNode.getNamespaceURI();
    String localName = namedNode.getLocalName();
    if (uri != null && uri.equals("http://www.w3.org/2000/xmlns/")) {
        for (String removeableNamespace : namespaces) {
            if (namedNode.getNodeValue().equals(removeableNamespace)) {
...
voidremoveNode(final Node node)
remove Node
if (node == null) {
    return;
final Node parent = node.getParentNode();
if (parent == null) {
    return;
parent.removeChild(node);
...