Java 使用Groovy从XML中删除所有空节点

Java 使用Groovy从XML中删除所有空节点,java,xml,groovy,xml-parsing,removechild,Java,Xml,Groovy,Xml Parsing,Removechild,我想使用groovy删除所有空节点 这是xml,我想删除电话和团队 <Profile> <Name>Fin</Name> <Aga>20</Aga> <Contact> <Mobile>1819</Mobile> <Telephone/> </Contact> <Team> &l

我想使用groovy删除所有空节点

这是xml,我想删除电话和团队

<Profile>
    <Name>Fin</Name>
    <Aga>20</Aga>
    <Contact>
        <Mobile>1819</Mobile>
        <Telephone/>
    </Contact>
    <Team>
        <Team1/>
        <Team2/>
    </Team>
</Profile>
但这不起作用,因为电话和团队没有被删除

这是输出

<Profile>
        <Name>Fin</Name>
        <Aga>20</Aga>
        <Contact>
            <Mobile>1819</Mobile>
            <Telephone/>
        </Contact>
        <Team>
            <Team1/>
            <Team2/>
        </Team>
  </Profile>

鳍
20
1819
这是预期的结果

<Profile>
    <Name>Fin</Name>
    <Aga>20</Aga>
    <Contact>
        <Mobile>1819</Mobile>
    </Contact>
</Profile>

鳍
20
1819

您能告诉我这方面的情况吗?

您可以按以下方式操作:

  • 查找节点文本为空的所有节点名称
  • 循环上述查找结果,并删除节点
def xmlString=”“”
鳍

Bell Aisaard,请检查解决方案,看看这是否解决了问题。只需请求@rao,如果您可以将代码的输出粘贴到答案中就好了。这节省了时间,因为我们不需要转到演示页面。尽管演示页面在我们想要编辑代码时非常有用感谢您的输入。如果您看到问题,OP已经阅读了y提到了期望的输出。但demo link的原因是他自己尝试,看看它是否工作,而且正如您正确指出的,可以修改某些内容,然后重试,而无需麻烦,并且可以快速完成。是的,它就在那里,但是回答问题的人可能会忽略某些事情,就像您记得在我的上一个问题中有一个Extra在发布的答案中,输出xml中出现了一行原始问题中没有的内容。因此,在我看来,最好是复制粘贴精确的输出
<Profile>
    <Name>Fin</Name>
    <Aga>20</Aga>
    <Contact>
        <Mobile>1819</Mobile>
    </Contact>
</Profile>