java JAXB 默认属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7909376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB default attribute value
提问by mishadoff
I'm using JAXB annotations to generate xsd schema from my classes.
我正在使用 JAXB 注释从我的类生成 xsd 模式。
Annotation @XmlElement with parameter defaultValue sets default value for element. Is it possible to set default value for @XmlAttribute?
带有参数 defaultValue 的注释 @XmlElement 设置元素的默认值。是否可以为@XmlAttribute 设置默认值?
P.S. I checked that xsd syntax allow default values for attributes
PS 我检查了 xsd 语法允许属性的默认值
回答by G_H
Might wanna check this: Does JAXB support default schema values?
可能想检查一下:Does JAXB support default schema values?
To be honest, I don't have a clue why there isn't an attribute default option in standard JAXB.
老实说,我不知道为什么标准 JAXB 中没有属性默认选项。
回答by thehpi
When you generate classes from an xsd where you define an attribute with a default value then jaxb will generate a if clause where it will check the null value and if so, will return the default value.
当您从 xsd 生成类时,您定义了一个具有默认值的属性,然后 jaxb 将生成一个 if 子句,它将检查空值,如果是,则返回默认值。
回答by Kumar Abhishek
For XML attributes default value goes inside getter method.
对于 XML 属性,默认值在 getter 方法中。
for Example,
例如,
customer.xsd
客户.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="Customer">
<complexType>
<sequence>
<element name="element" type="string" maxOccurs="1" minOccurs="0" default="defaultElementName"></element>
</sequence>
<attribute name="attribute" type="string" default="defaultAttributeValue"></attribute>
</complexType>
</element>
</schema>
It will generate class like below.
它将生成如下所示的类。
@XmlRootElement(name = "Customer")
public class Customer {
@XmlElement(required = true, defaultValue = "defaultElementName")
protected String element;
@XmlAttribute(name = "attribute")
protected String attribute;
......
public String getAttribute() {
//here the default value is set.
if (attribute == null) {
return "defaultAttributeValue";
} else {
return attribute;
}
}
Created Sample XML to read
创建了要读取的示例 XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Customer><element/></Customer>
when we write logic to marshall in our main class.
当我们在主类中编写逻辑来编组时。
File file = new File("...src/com/testdefault/xsd/CustomerRead.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer.getElement());
System.out.println(customer.getAttribute());
It will print, in console. defaultElementName defaultAttributeValue
它将在控制台中打印。defaultElementName defaultAttributeValue
P.S -: to get default value of elements you need to have a blank copy of element into xml which is being marshalled.
PS -:要获得元素的默认值,您需要将元素的空白副本放入正在编组的 xml 中。