如何删除输入xml中的空标记

SGB*_*SGB 5 java xml jaxb

我的java模块从大型机获取一个巨大的输入xml.不幸的是,大型机无法跳过可选元素,结果我在输入中得到了很多空标记:

所以,

<SSN>111111111</SSN>
<Employment>
<Current>
<Address>
<line1/>
<line2/>
<line3/>
<city/>
<state/>
<country/>
</Address>
<Phone>
<phonenumber/>
<countryCode/>
</Phone>
</Current>
<Previous>
<Address>
<line1/>
<line2/>
<line3/>
<city/>
<state/>
<country/>    
</Address>
<Phone>
<phonenumber/>
<countryCode/>
</Phone>
</Previous>
</Employment>
<MaritalStatus>Single</MaritalStatus>
Run Code Online (Sandbox Code Playgroud)

应该:

<SSN>111111111</SSN>
<MaritalStatus>SINGLE</MaritalStatus>
Run Code Online (Sandbox Code Playgroud)

我使用jaxb解组大型机发送的输入xml字符串.是否有一种干净/简单的方法来删除所有空组标签,或者我必须在每个元素的代码中执行此操作.我的输入xml中有超过350个元素,所以如果jaxb本身有办法自动执行此操作,我会很喜欢它吗?

谢谢,SGB

bli*_*app 5

您可以使用 XSLT 进行预处理。我知道它现在被认为有点“迪斯科”,但它快速且易于应用。

从这个tek-tips讨论中,您可以使用 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>
Run Code Online (Sandbox Code Playgroud)