我在使用 jaxb 创建枚举以生成我想要的 xml 时遇到问题,我尝试使用 @xmlEnum 注释但没有使用属性!
我会给你举个例子来阐明这一点:
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>toto@sgcib.com</MailTo>
<MailFrom>titi@sgcib.com</MailFrom>
<Subject>tata</Subject>
<Body>blabla</Body>
</MailSend>
</Event>
</Match>
</Directory>
</FilePollerConfiguration>
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;
}
}
因此,如果您有任何想法,不客气 =)
谢谢。
最佳答案
尝试向您的类添加另一个字段并将其标记为@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 ...)
如果我正确理解了您的需求,这应该可以解决您的问题。
再见!
更新(使用 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>
您可以看到 complexType
event
有一个名为“name”的属性,类型为“name”,类型名称定义为 simpleType
使用枚举
。
最终更新:
这是我写的:
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);
}
}
如果执行main方法,结果如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<event name="create"/>
这是一个带有来自枚举的属性的事件标签......
我希望这最后的尝试能帮到你......
关于java - @XmlEnum 属性与 JAXB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19537320/