7

I want to delete the empty nodes from an XML element. This xml is generated from a vendor and i dont have control on xml generation. But since the XML has few empty nodes i need to delete those empty nodes recursively.

This xml is got from OMElement and i get an Element from this object using [XMLUtils][1] Sample XML

<A>
  <B>
    <C>
      <C1>
        <C11>something</C11>
        <C12>something</C12>
      </C1>
    </C>
    <D>
      <D1>
        <D11>
          <D111 operation="create">
            <Node>something else</Node>
          </D11>
        </D11>
      </D1>
      <D2>
        <D21>

        </D21>
      </D2>
    </D>
  </B>
</A> 

Since D21 is an empty node i want to delete D21 and since now D2 is an empty node i want to delete D2 but since D has D1 i dont want to delete D.

Similarly it is possible that i can get

<A>
  <B>
    <C>

    </C>
  </B>
</A>

Now since C is empty i want to delete C and then B and then eventually node A. I am trying to do this using removeChild() method in Node

But so far i am unable to remove them recursively. Any suggestions to remove them recursively?

I am recursively trying to get node and node length. But node length is of no help

if(childNode.getChildNodes().getLength() == 0 ){
       childNode.getParentNode().removeChild(childNode);

               }

Regards
Dheeraj Joshi

3
  • and what is the code you have written for this
    – Satya
    Sep 21, 2012 at 5:30
  • 1
    If childNode.getChildNodes().getLength() == 0, it means it has no child nodes. If you want to find empty node then try to get node value of the node with no child and if it returns null or equivalent value then you can delete that node. Sep 21, 2012 at 5:51
  • No. nodevalue is null for all elements. So checking the node value is of no help Sep 21, 2012 at 6:06

5 Answers 5

7

I don't have enough rep to comment on @Adam's solution, but I was having an issue where after a node removal, the last sibling of that node was moved to index zero, causing it to not fully remove empty elements. The fix was to use a list to hold all of the nodes we want to recursively call for removal.

Also, there was a bug that removed empty elements that had attributes.

Solution to both issues:

public static void removeEmptyNodes(Node node) {

    NodeList list = node.getChildNodes();
    List<Node> nodesToRecursivelyCall = new LinkedList();

    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) {
        if(!node.hasAttributes()) {
            node.getParentNode().removeChild(node);
        }
    }

}
1
  • I can't believe this answer not got much upvotes, works superbly fine and without any transforming manipulating xml. I searched for so many solution, this one worked perfectly for me.. thank you
    – Akhil Jain
    Jul 21, 2017 at 11:59
4

This works, just create a recursive function that "goes deep" first, then removes empty nodes on the way "back up the tree", this will have the effect of removing both D21 and D2.

public static void main(String[] args) throws Exception {

    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    String input = "<A><B><C><C1><C11>something</C11><C12>something</C12></C1></C><D><D1><D11><D111 operation=\"create\"><Node>something else</Node></D111></D11></D1><D2><D21></D21></D2></D></B></A>";

    Document document = builder.parse(new InputSource(new StringReader(
            input)));

    removeNodes(document);

    Transformer transformer = TransformerFactory.newInstance()
            .newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(new StringWriter());
    transformer.transform(new DOMSource(document), result);
    System.out.println(result.getWriter().toString());
}

public static void removeNodes(Node node) {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        removeNodes(list.item(i));
    }
    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);
    }
}

Output

<A>
<B>
<C>
<C1>
<C11>something</C11>
<C12>something</C12>
</C1>
</C>
<D>
<D1>
<D11>
<D111 operation="create">
<Node>something else</Node>
</D111>
</D11>
</D1>
</D>
</B>
</A>
4
  • thank for responsing but i was hoping to remove <D2></D2> if it is empty. But as per your code and the code which i have i still get <D2></D2> empty nodes Sep 21, 2012 at 8:30
  • @DheerajJoshi I've added the output of my solution, it does remove the D2 node...
    – Adam
    Sep 21, 2012 at 8:43
  • @Adam your solution has small error: node.getChildNodes().getLength() == 0 && node.getNodeType() == Node.ELEMENT_NODE) It will not work for nodes with some (maybe just one space) text. Because parser will create for such nodes addition inner node with type Node.TEXT_NODE. Correct condition should include additional check || node.getNodeType() == Node.TEXT_NODE && StringUtils.isBlank(node.getNodeValue())) Sep 21, 2012 at 12:28
  • 1
    @user1516873 I've updated my solution to deal with nodes containing white space.
    – Adam
    Sep 21, 2012 at 16:00
2

Use getTextContent() on top-level element of DOM. If method return empty string or null, you can removed this node, because this node and all child nodes is empty. If method getTextContent() return not empty string, call getTextContent on every child of current node, and so on.
See documentation.

2

Just work with strings:

    Pattern emptyValueTag = Pattern.compile("\\s*<\\w+/>");
    Pattern emptyTagMultiLine = Pattern.compile("\\s*<\\w+>\n*\\s*</\\w+>");

    xml = emptyValueTag.matcher(xml).replaceAll("");

    while (xml.length() != (xml = emptyTagMultiLine.matcher(xml).replaceAll("")).length()) {
    }

    return xml;
0
public class RemoveEmprtElement {

public static void main(String[] args) {
    ReadFile readFile =new ReadFile();
    String strXml=readFile.readFileFromPath(new File("sampleXml4.xml"));
    RemoveEmprtElement elementEmprtElement=new RemoveEmprtElement();
    DocumentBuilder dBuilder = null;
    Document doc = null;
    try {
        dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = dBuilder.parse(new ByteArrayInputStream(strXml.getBytes()));

        elementEmprtElement.getEmptyNodes(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        StreamResult result = new StreamResult(new StringWriter());
        trans.transform(new DOMSource(doc), result);
        System.out.println(result.getWriter().toString());

    }catch(Exception e) {
        e.printStackTrace();
    }
}

private void getEmptyNodes(Document doc){

    try {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("//*[not(*)]");
        Object resultNS = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) resultNS;
        for(int i =0 ; i < nodes.getLength() ; i++){
            Node node = nodes.item(i);
            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) {
                xmlNodeRemove(doc,findPath(node));
                getEmptyNodes(doc);
            }
        } 
    }catch(Exception e) {
        e.printStackTrace();
    }

}

private void xmlNodeRemove(Document doc,String xmlNodeLocation){

    try {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile(xmlNodeLocation);
        Object resultNS = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) resultNS;
        Node node =nodes.item(0);
        if(node!=null && node.getParentNode()!=null && node.getParentNode().hasChildNodes()){
        node.getParentNode().removeChild(node);
        }
    }catch(Exception e) {
        e.printStackTrace();
    }
}

private String findPath(Node n) {
    String path="";
    if(n==null){
        return path;
    }else if(n.getNodeName().equals("#document")){
        return "";
    }
        else{
            path=n.getNodeName();
            path=findPath(n.getParentNode())+"/"+path;
        }
        return path;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.