Java 带有 JAXB 的属性上的 @XmlEnum
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19537320/
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
@XmlEnum on attribute with JAXB
提问by Julien Moreau
I have a problem to create enum with jaxb to generate the xml that i want, i tried to use the @xmlEnum annotation but not with an attribute !
我在用 jaxb 创建枚举以生成我想要的 xml 时遇到问题,我尝试使用 @xmlEnum 注释但没有使用属性!
I'll give you the example to clarify that :
我会给你一个例子来澄清这一点:
XML
XML
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<FilePollerConfiguration configFilePath="">
<Directory path="C://Users//jmoreau040612//Desktop//Old">
<Match pattern="*.xml">
<Event name="create | modify | delete"> //here i want the enum in attribute
<FTPSend>
<FTPServer>toto.sgcib.com</FTPServer>
<FTPPort>21</FTPPort>
<FTPLogin>toto</FTPLogin>
<FTPPassword>titi</FTPPassword>
<FTPDestinationPath>/root/src</FTPDestinationPath>
</FTPSend>
</Event>
</Match>
<Match pattern="*.csv">
<Event name="create | modify | delete"> //here i want the enum in attribute
<MailSend>
<SMTPServer>smtp.fr.socgen</SMTPServer>
<SMTPPort>25</SMTPPort>
<MailTo>[email protected]</MailTo>
<MailFrom>[email protected]</MailFrom>
<Subject>tata</Subject>
<Body>blabla</Body>
</MailSend>
</Event>
</Match>
</Directory>
</FilePollerConfiguration>
And i have the following code for tha javapart :
我有以下java部分的代码:
@XmlAccessorType(XmlAccessType.FIELD)
public class Event {
//I would like this enum in attribute of "Event"
@XmlType
@XmlEnum(String.class)
public enum name{
@XmlEnumValue("create") CREATE,
@XmlEnumValue("modify") MODIFY,
@XmlEnumValue("delete") DELETE
}
@XmlElements(value = {
@XmlElement(type=FTPSendConfiguration.class, name="FTPSend"),
@XmlElement(type=SFTPSendConfiguration.class, name="SFTPSend"),
@XmlElement(type=MailSendConfiguration.class, name="MailSend"),
@XmlElement(type=
ServerToServerSendConfiguration.class, name="ServerToServer")
})
ArrayList<IAction> actionsList = new ArrayList<IAction>();
public Event(){
}
public ArrayList<IAction> getActionsList() {
return actionsList;
}
public void setActionsList(ArrayList<IAction> actionsList) {
this.actionsList = actionsList;
}
}
So if you have an idea you're welcome =)
因此,如果您有想法,欢迎您=)
Thanks.
谢谢。
采纳答案by Paolo
Try to add to your class another field and mark it as @XmlAttribute
:
尝试向您的班级添加另一个字段并将其标记为@XmlAttribute
:
@XmlAttribute(name="name") // "name" will be the name of your attribute in the xml file
name eventAttribute; // this field has type name (your enum that probably should be better to call in a different way ...)
If I correctly understand what you need, this should resolve your problem.
如果我正确理解您的需求,这应该可以解决您的问题。
Ciao!
再见!
UPDATE(.xsd file generated using schemagen):
更新(使用 schemagen 生成的 .xsd 文件):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="event">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="FTPSend" type="xs:string"/>
<xs:element name="SFTPSend" type="xs:string"/>
<xs:element name="MailSend" type="xs:string"/>
<xs:element name="ServerToServer" type="xs:string"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="name" type="name"/>
</xs:complexType>
<xs:simpleType name="name">
<xs:restriction base="xs:string">
<xs:enumeration value="create"/>
<xs:enumeration value="modify"/>
<xs:enumeration value="delete"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
You can see that the complexType
event
has an attribute named "name" of type "name" and the type name is defined like a simpleType
using enumeration
.
您可以看到complexType
event
有一个名为“name”的属性,类型为“name”,并且类型名称的定义类似于simpleType
using enumeration
。
FINAL UPDATE:
最终更新:
This is what I wrote:
这是我写的:
package jaxb;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Event {
// I would like this enum in attribute of "Event"
@XmlType
@XmlEnum(String.class)
public enum name {
@XmlEnumValue("create")
CREATE, @XmlEnumValue("modify")
MODIFY, @XmlEnumValue("delete")
DELETE
}
@XmlAttribute(name = "name")
name eventAttribute;
public name getEventAttribute() {
return eventAttribute;
}
public void setEventAttribute(name eventAttribute) {
this.eventAttribute = eventAttribute;
}
@XmlElements(value = {
@XmlElement(type = FTPSendConfiguration.class, name = "FTPSend"),
@XmlElement(type = SFTPSendConfiguration.class, name = "SFTPSend"),
@XmlElement(type = MailSendConfiguration.class, name = "MailSend"),
@XmlElement(type = ServerToServerSendConfiguration.class, name = "ServerToServer") })
ArrayList<IAction> actionsList = new ArrayList<IAction>();
public Event() {
}
public ArrayList<IAction> getActionsList() {
return actionsList;
}
public void setActionsList(ArrayList<IAction> actionsList) {
this.actionsList = actionsList;
}
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Event.class);
Event e = new Event();
e.eventAttribute = name.CREATE; // I set this field using the enum (it could be set to name.DELETE or name.MODIFY as well)
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(e, System.out);
}
}
If you execute the main method, the result will be the following:
如果执行main方法,结果如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<event name="create"/>
That is an event tag with an attribute coming out from an enum ...
那是一个带有来自枚举的属性的事件标记......
I hope this last try will help you ...
我希望这最后一次尝试会帮助你......