I assume that the empty elements are actually ELEMENT_NODE without any child elements in the document. Try adding empty node text instead. This can trick the author into believing that there is node text there, so he will write it as if it were one. But node text does not output anything because it is an empty string.
Calling this method with a document, since both parameters should do the trick:
private static void fillEmptyElementsWithEmptyTextNodes( final Document doc, final Node root) { final NodeList children = root.getChildNodes(); if (root.getType() == Node.ELEMENT_NODE && children.getLength() == 0) { root.appendChild(doc.createTextNode("")); }
source share