Java XML Element Remove removeEmptyElements(Node node)

Here you can find the source of removeEmptyElements(Node node)

Description

Takes the supplied node and recursively removes empty (no content/child nodes) elements

License

Open Source License

Parameter

Parameter Description
node to remove all empty elements from

Exception

Parameter Description
XPathExpressionException an exception

Return

Trimmed node

Declaration

public static Node removeEmptyElements(Node node) throws XPathExpressionException 

Method Source Code


//package com.java2s;
/*//ww  w  . j a v  a2 s  . c o m
 * Copyright (c) 2004-2012 The YAWL Foundation. All rights reserved.
 * The YAWL Foundation is a collaboration of individuals and
 * organisations who are committed to improving workflow technology.
 *
 * This file is part of YAWL. YAWL is free software: you can
 * redistribute it and/or modify it under the terms of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 *
 * YAWL is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
 * Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with YAWL. If not, see <http://www.gnu.org/licenses/>.
 */

import org.w3c.dom.*;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class Main {
    /**
     * Takes the supplied node and recursively removes empty (no content/child nodes) elements
     *
     * @param node to remove all empty elements from
     * @return Trimmed node
     * @deprecated
     * @throws XPathExpressionException
     */
    public static Node removeEmptyElements(Node node) throws XPathExpressionException {

        NodeList list = selectNodeList(node, "*[string-length(normalize-space(.)) = 0]");

        for (int i = 0; i < list.getLength(); i++) {
            node.removeChild(list.item(i));
        }

        if (node.hasChildNodes()) {
            NodeList childs = node.getChildNodes();
            for (int i = 0; i < childs.getLength(); i++) {
                if (childs.item(i) instanceof Element) {
                    removeEmptyElements(childs.item(i));
                }
            }
        }

        return node;
    }

    public static NodeList selectNodeList(Node node, String expression) throws XPathExpressionException {
        XPathFactory factory = XPathFactory.newInstance();
        XPath path = factory.newXPath();
        Object result = path.evaluate(expression, node, XPathConstants.NODESET);
        return (NodeList) result;
    }
}

Related

  1. remove(Element element, Predicate shouldRemovePredicate)
  2. removeAll(Element node, String subElement)
  3. removeContent(Element element)
  4. removeElements(Element from, String named)
  5. removePreviousSiblingText(Element element)
  6. removeSchemaLocation(Element el)
  7. removeTopPi(Element e, Element pi)
  8. removeWhitespaceNodes(Element e)