JAXB避免保存默认值

5
有没有办法让JAXB不保存那些值与@Element注释中指定的默认值相同的字段,然后在从XML加载为空或空白元素时设置值?例如:
class Example
{
    @XmlElement(defaultValue="default1")
    String prop1;
}

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

应该生成:

<example/>

当加载时

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

我正在尝试这样做是为了生成一个干净的XML配置文件,使其更易读且文件大小更小。

谢谢您的帮助。


我不相信JAXB会检查值是否等于默认值,因此应该跳过。第二个片段应该可以正常工作。断言失败了吗? - dma_k
2个回答

3

通过利用XmlAccessorType(XmlAccessType.FIELD)并在get/set方法中放置逻辑,您可以像以下示例一样执行某些操作:

示例

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;
        }
    }

}

演示

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);
    }

}

输出

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>

更多信息


恰恰相反,我想避免“所有”代码,并让JAXB在加载和保存文档时自动完成。无论如何,我该如何处理基本类型(即避免保存int,因为我无法使它们为空)?将它们声明为Integer吗? - okelet

0

对于编程解决方案,还有老牌的Apache commons XmlSchema,您可以使用XmlSchemaElement.getDefaultValue()检查默认值。

因此,可以使用以下类似代码:

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

你应该能够做你需要的事情。最后没有尝试,因为我需要一个更直接的解决方案,但希望这能帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接