如何让JAXB排除空节点?

时间:2014-05-23 20:44:14

标签: xml jaxb marshalling xmladapter

我正在使用JAXB将对象序列化为XML。现在我意识到它将从响应中排除自然空值,但不会从自定义适配器中排除空值。以下是我的代码。

package com.model;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class JAXBTest {

public static void main(String[] args) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(Thing.class);

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Thing thing = new Thing();
    thing.setName("popcorn ball");

    thing.setAwesome(false);
    marshaller.setAdapter(BooleanAdapter.class,new BooleanAdapter(true));
    marshaller.marshal(thing, System.out);

}

@XmlRootElement 
public static class Thing{
    private String name;
    private Boolean awesome;
    private String empty;

    public String getEmpty() {
        return empty;
    }
    public void setEmpty(String empty) {
        this.empty = empty;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }

    public void setAwesome(Boolean awesome) {
        this.awesome = awesome;
    }
    public Boolean isAwesome() {
        return this.awesome;
    }
}

public static class BooleanAdapter extends XmlAdapter<String, Boolean> {

    boolean flag;

    public BooleanAdapter(boolean flag){
        this.flag = flag;
    }

    public Boolean unmarshal(String v) throws Exception {
        return Boolean.TRUE.equals(v);
    }

    public String marshal(Boolean v) throws Exception {
        if(flag){
             if(v) {
                return v.toString();
            }
            return null;
        }else{
            return v.toString();
        }          
    }
}
}

packageinfo.java:

@XmlJavaTypeAdapter(value=JAXBTest.BooleanAdapter.class, type=Boolean.class)
package com.model;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<thing>
    <awesome/>
    <name>popcorn ball</name>
</thing>

如您所见,即使我在适配器中返回null,响应中仍有一个空节点 awesome / 。完全忽略自然空字段“ private String empty ”。所以我的问题是,如何从响应中排除这些空节点?

1 个答案:

答案 0 :(得分:0)

对于元素使用空类而不是String,只对您的类调整TRUE。 (也许new Object()可行,但我没试过。)

public class BooleanXmlElement {

    public BooleanXmlElement() {
    }


    public static class Adapter extends XmlAdapter<BooleanXmlElement, Boolean> {

        public Boolean unmarshal(BooleanXmlElement v) {
            return Boolean.TRUE;
        }

        public BooleanXmlElement marshal(Boolean v) {
            if (Boolean.TRUE.equals(v)) {
                return new BooleanXmlElement();
            }
            return null;
        }
    }
}

这对于属性没有用,但对于它们来说,String似乎是必要的:

public class BooleanXmlAttribute extends XmlAdapter<String, Boolean> {

    public Boolean unmarshal(String v) {
        return Boolean.valueOf(v);
    }

    public String marshal(Boolean v) {
        if (Boolean.TRUE.equals(v)) {
            return Boolean.TRUE.toString();
        }
        return null;
    }
}

同时涵盖Booleanboolean注释您的包裹:

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(value=BooleanXmlElement.Adapter.class, type=boolean.class),
    @XmlJavaTypeAdapter(value=BooleanXmlElement.Adapter.class, type=Boolean.class)
})
package com.model;

编辑:删除行:

marshaller.setAdapter(BooleanAdapter.class,new BooleanAdapter(true));

你不需要它。