JAXB 避免保存默认值

发布于 2024-12-27 05:27:03 字数 569 浏览 3 评论 0原文

有没有办法让 JAXB 不保存值是 @Element 注释中指定的默认值的字段,然后在从 XML 加载 null 或空元素时设置该值?一个例子:

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 配置文件,并使其具有更好的可读性和更小的尺寸。

提前致以问候和感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不语却知心 2025-01-03 05:27:03

您可以通过利用 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>

了解更多信息

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

情深已缘浅 2025-01-03 05:27:03

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

因此,

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

您应该能够执行您需要的操作。最终没有尝试过,因为我需要一个更直接的解决方案,但我希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文