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 !
I'll give you the example to clarify that :
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 java part :
@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.
Try to add to your class another field and mark it as @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):
<?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
.
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:
<?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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With