Java删除空XML标记

时间:2009-11-06 12:13:11

标签: java xml tags

我正在寻找一个简单的Java代码段来从(任意)XML结构中删除空标记

<xml>
    <field1>bla</field1>
    <field2></field2>
    <field3/>
    <structure1>
       <field4>bla</field4>
       <field5></field5>
    </structure1>
</xml>

应该变成;

<xml>
    <field1>bla</field1>
    <structure1>
       <field4>bla</field4>
    </structure1>
</xml>

9 个答案:

答案 0 :(得分:8)

此XSLT样式表应该可以满足您的需求:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:if test=". != '' or ./@* != ''">
      <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

它应该保留空的但具有不属性的元素。如果您不想要此行为,请更改:

<xsl:if test=". != '' or ./@* != ''">

收件人:<xsl:if test=". != ''">

如果你想知道如何在Java中应用XSLT,那么Interwebs上应该有很多教程。祝你好运!

答案 1 :(得分:7)

我想知道用XOM库做这件事是否容易,并尝试了一下。

结果很简单:

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));
        }
    }
}

这可能无法正确处理所有角落案例,就像完全空的文档一样。如何处理空白但具有属性的元素呢?

如果要使用属性保存XML标记,我们可以在方法'handleNode'中添加以下检查:

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

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

(这个答案是我对XOM评估为潜在replacement to dom4j的一部分。)

答案 2 :(得分:3)

作为旁注:标签的不同状态实际上具有含义:

  • 打开 - 关闭标记:元素存在且其值为空字符串
  • 单个标记:元素存在,但值为nullnil
  • 缺少标记元素不存在

因此,通过删除空的Open-Closed标记和单个标记,您将它们与缺失标记组合并,从而丢失信息。

答案 3 :(得分:2)

我测试了Jonik&和Marco的示例代码。但那些并不是我想要的。所以我修改了它们的源码,下面的代码对我来说效果很好。我已经在我的项目中调整了这段代码。如果你愿意,请测试一下。

public String removeEmptyNode(String xml){
    String cleanedXml = null;
    try{
        xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + xml;
        InputStream input = new ByteArrayInputStream(xml.getBytes("UTF-8"));
        Document document = new Builder().build(input);
        removeEmptyNode(document.getRootElement());
        cleanedXml = document.toXML();
    }catch(Exception e){
        e.printStackTrace();
    }
    return cleanedXml;
}

private static void removeEmptyNode(Node node) {
    if(node.getChildCount()!=0){
        int count = node.getChildCount();
        for (int i = count-1; i >= 0 ; i--) { 
            removeEmptyNode(node.getChild(i));
        }
    }

    doCheck(node);
}

private static void doCheck(Node node){
    if(node.getChildCount() == 0 && "".equals(node.getValue().trim())) {
        try{node.getParent().removeChild(node);}catch(Exception e){}
    }       
}

答案 4 :(得分:1)

如果xml以字符串形式提供;正则表达式可用于过滤掉空元素:

<(\\w+)></\\1>|<\\w+/>

这会找到空元素。

data.replaceAll(re, "")
在这种情况下,

数据是一个包含xml字符串的变量。
不是说这是最好的解决方案,但它有可能......

答案 5 :(得分:1)

我需要在Chris R的答案中添加strip-space和indent元素,否则不会删除新封闭的块:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes" />
  <xsl:template match="@*|node()">
    <xsl:if test=". != '' or ./@* != ''">
      <xsl:copy>
        <xsl:apply-templates  select="@*|node()"/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

答案 6 :(得分:0)

使用XSLT,您可以转换XML以忽略空标记并重新编写文档。

答案 7 :(得分:0)

要删除所有空标记,即使它们是一个接一个,一个可能的解决方案是:

 private void removeEmptyTags(Document document) {
    List<Node> listNode = new ArrayList<Node>();
    findListEmptyTags(document.getRootElement(), listNode);
    if (listNode.size() == 0)
        return;

    for (Node node : listNode) {
        node.getParent().removeChild(node);
    }
    removeEmptyTags(document);
}

private void findListEmptyTags(Node node, List<Node> listNode) {

    if (node != null && node.getChildCount() == 0 && "".equals(node.getValue()) && ((Element) node).getAttributeCount() == 0) {
        listNode.add(node);
        return;
    }
    // recurse the children
    for (int i = 0; i < node.getChildCount(); i++) {
        findListEmptyTags(node.getChild(i), listNode);
    }
}

答案 8 :(得分:0)

public static void main(String[] args) {

    final String regex1 = "<([a-zA-Z0-9-\\_]*)[^>]*/>";
    final String regex2 = "<([a-zA-Z0-9-\\_]*)[^>]*>\\s*</\\1>";

    String xmlString = "<xml><field1>bla</field1><field2></field2><field3/><structure1><field4><field50><field50/></field50></field4><field5></field5></structure1></xml>";
    System.out.println(xmlString);

    final Pattern pattern1 = Pattern.compile(regex1);
    final Pattern pattern2 = Pattern.compile(regex2);

    Matcher matcher1;
    Matcher matcher2;
    do { 
        xmlString = xmlString.replaceAll(regex1, "").replaceAll(regex2, "");
        matcher1 = pattern1.matcher(xmlString);
        matcher2 = pattern2.matcher(xmlString);
    } while (matcher1.find() || matcher2.find());

    System.out.println(xmlString);
}

控制台:

<xml>
    <field1>bla</field1>
    <field2></field2>
    <field3/>
    <structure1>
        <field4>
            <field50>
                <field60/>
            </field50>
        </field4>
        <field5></field5>
    </structure1>
</xml>

<xml>
    <field1>bla</field1>
</xml>

Online demo here

相关问题