简体   繁体   English

JAXB避免保存默认值

[英]JAXB Avoid saving default values

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? 是否有任何方法可以使JAXB不保存哪些值是@Element注释中指定的默认值,然后在从XML加载null或空的元素时将值设置为它? 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. 我试图这样做是为了生成一个干净的XML配置文件,并使其更好的可读性和更小的尺寸。

Regars and thanks in advance. 提前退位并表示感谢。

You could do something like the following by leveraging XmlAccessorType(XmlAccessType.FIELD) and putting logic in the get/set methods: 您可以通过利用XmlAccessorType(XmlAccessType.FIELD)并将逻辑放入get / set方法来执行以下操作:

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 欲获得更多信息

For a programmatic solution, there's also good old Apache commons XmlSchema and you can check against the default value with XmlSchemaElement.getDefaultValue() 对于程序化解决方案,还有很好的旧Apache公共XmlSchema ,您可以使用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. 最后没有尝试过,因为我需要更直接的解决方案,但我希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM