且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java删除空XML标记

更新时间:2023-02-15 11:45:33

我想知道这样做是否容易使用 XOM 库并试一试。

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

结果很简单:

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?

如果要保存带有属性的XML标记,我们可以在方法'handleNode'中添加以下内容检查:

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

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

此外,如果xml有两个或多个空标签,一个接一个;这个递归方法不会删除所有空标记!

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

(这个答案是我评估XOM作为潜在的替换为dom4j 。)

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