JAXB Avoid saving default values

删除回忆录丶 提交于 2019-12-01 04:24:07

问题


Is there any way to make JAXB not save fields which values are the default values specified in the @Element annotations, and then make set the value to it when loading elements from XML that are null or empties? An example:

class Example
{
    @XmlElement(defaultValue="default1")
    String prop1;
}

Example example = new Example();
example.setProp1("default1");
jaxbMarshaller.marshal(example, aFile);

Should generate:

<example/>

And when loading

Example example = (Example) jaxbUnMarshaller.unmarshal(aFile);
assertTrue(example.getProp1().equals("default1"));

I am trying to do this in order to generate a clean XML configuration file, and make it better readable and smaller size.

Regars and thanks in advance.


回答1:


You could do something like the following by leveraging XmlAccessorType(XmlAccessType.FIELD) and putting logic in the get/set methods:

Example

package forum8885011;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Example {

    private static final String PROP1_DEFAULT = "default1";
    private static final String PROP2_DEFAULT = "123";

    @XmlElement(defaultValue=PROP1_DEFAULT)
    String prop1;

    @XmlElement(defaultValue=PROP2_DEFAULT)
    Integer prop2;

    public String getProp1() {
        if(null == prop1) {
            return PROP1_DEFAULT;
        }
        return prop1;
    }

    public void setProp1(String value) {
        if(PROP1_DEFAULT.equals(value)) {
            prop1 = null;
        } else {
            prop1 = value;
        }
    }

    public int getProp2() {
        if(null == prop2) {
            return Integer.valueOf(PROP2_DEFAULT);
        }
        return prop2;
    }

    public void setProp2(int value) {
        if(PROP2_DEFAULT.equals(String.valueOf(value))) {
            prop2 = null;
        } else {
            prop2 = value;
        }
    }

}

Demo

package forum8885011;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Example.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Example example = new Example();
        example.setProp1("default1");
        example.setProp2(123);
        System.out.println(example.getProp1());
        System.out.println(example.getProp2());
        marshaller.marshal(example, System.out);

        example.setProp1("FOO");
        example.setProp2(456);
        System.out.println(example.getProp1());
        System.out.println(example.getProp2());
        marshaller.marshal(example, System.out);
    }

}

Output

default1
123
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<example/>
FOO
456
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<example>
    <prop1>FOO</prop1>
    <prop2>456</prop2>
</example>

For More Information

  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html



回答2:


For a programmatic solution, there's also good old Apache commons XmlSchema and you can check against the default value with XmlSchemaElement.getDefaultValue()

So with something like

XmlSchemaElement elem = schema.getElementByName(ELEMENT_QNAME);
String defval = elem.getDefaultValue();

you should be able to do what you need. Haven't tried it out in the end, because I needed a more direct solution, but I hope that helps.



来源:https://stackoverflow.com/questions/8885011/jaxb-avoid-saving-default-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!