如果对象中的所有内容都为空,则避免使用XML中的空子标记 [英] Avoid Empty Sub-Tags in XML if everything is blank in an object

查看:301
本文介绍了如果对象中的所有内容都为空,则避免使用XML中的空子标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有JAXB对象marshing / unmarshalling XML。目前,我们得到这个标签,即使其中的所有内容都是空的。 如果所有子元素都为空,我们需要完全摆脱Top部分。(如果至少有一个不是空的,它应该存在。)

We have JAXB objects marshing/unmarshalling XML. Currently, we get this tag, even though everything inside it is empty. We need to completely get rid of the Top section if all its sub-elements are empty. (If at least one is not empty, it should exist.)

    <ns2:Top>
        <ns2:OrganizationName></ns2:OrganizationName>
        <ns2:Address>
            <ns2:Street1></ns2:Street1>
            <ns2:Street2></ns2:Street2>
            <ns2:City></ns2:City>
            <ns2:ZipPostalCode></ns2:ZipPostalCode>
        </ns2:Address>
    </ns2:Top>

JAXB类定义为:

    @XmlElement(name = "Top")
    protected Top top;


推荐答案

您可以控制编组中出现的元素数量通过设置或不设置字段来设置XML。

You can control the amount of elements appearing in the marshalled XML by setting or not setting fields.

Root root = new Root();
root.setTop( new Top() );                   // (1)
root.getTop().setAddress( new Address() );  // (2)

使用第(1)和(2)行,编组产生

With lines (1) and (2), marshalling produces

<root><top><address/></top></root>

只有第(1)行,XML将

With just line (1), the XML will be

<root><top/></root>

省略两行收益

<root/>

一旦你将organizationName或地址设置为Top,并且将一些或所有String字段设置为空string(即)你必须编写一些代码来修剪无用的字段。 (如果没有可用的有用值,将字段设置为空字符串是非常值得怀疑的。)

Once you have organizationName or address or both set in Top with some or all String fields set to the empty string (i.e., "") you'll have to write some code for pruning the useless fields. (Although it is highly questionable to set fields to the empty string if there isn't a useful value to be used.)

稍后适配器是个好主意。注释要保留的元素,顶部或上面的元素:

Later An adapter is a good idea. Annotate the element you want to keep, Top, or the one above it:

@XmlElement
@XmlJavaTypeAdapter(Adapter.class)
protected Top top;

并编写一个适配器来调查对象层次结构,将其替换为空的顶级元素:

And write an adapter to investigate the object hierarchy, replacing it with an empty top level element:


public class Adapter extends XmlAdapter {
    public Top unmarshal( Top v ) throws Exception {
        return v;
    }

    private boolean allEmpty( String... strings ){
        for( String s: strings ){
            if( s != null && ! s.equals( "" ) ) return false;
        }
        return true;
    }

    public Top marshal( Top top ) throws Exception {
        Address a = top.getAddress();
        return allEmpty( top.getOrganizationName(),
                         a.getStreet1(), a.getStreet1(),
                         a.getCity(), a.getPostalCode(),
                         a.getState(), a.getCountry() ) ? new Top() : top;
    }
}

我选择了Top,所以你保持空<顶部> ,但你可以更高一级。

I have chosen Top, so you keep an empty <top>, but you can do this one level higher.

这篇关于如果对象中的所有内容都为空,则避免使用XML中的空子标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆