Java Remove empty XML tags

前端 未结 9 2384
死守一世寂寞
死守一世寂寞 2020-12-11 16:57

I\'m looking for a simple Java snippet to remove empty tags from a (any) XML structure


    bla
    <         


        
9条回答
  •  星月不相逢
    2020-12-11 17:46

    I was wondering whether it would be easy to do this with the XOM library and gave it a try.

    It turned out to be quite easy:

    import nu.xom.*;
    
    import java.io.File;
    import java.io.IOException;
    
    public class RemoveEmptyTags {
    
        public static void main(String[] args) throws IOException, ParsingException {
            Document document = new Builder().build(new File("original.xml"));
            handleNode(document.getRootElement());
            System.out.println(document.toXML()); // empty elements now removed
        }
    
        private static void handleNode(Node node) {
            if (node.getChildCount() == 0 && "".equals(node.getValue())) {
                node.getParent().removeChild(node);
                return;
            }
            // recurse the children
            for (int i = 0; i < node.getChildCount(); i++) { 
                handleNode(node.getChild(i));
            }
        }
    }
    

    This probably won't handle all corner cases properly, like a completely empty document. And what to do about elements that are otherwise empty but have attributes?

    If you want to save XML tags with attributes, we can add in the method 'handleNode' the following check:

    ... && ((Element) node).getAttributeCount() == 0) )
    

    Also, if the xml has two or more empty tags, one after another; this recursive method doesn't remove all empty tags!

    (This answer is part of my evaluation of XOM as a potential replacement to dom4j.)

提交回复
热议问题