繁体   English   中英

使用JAXB的@XmlEnum属性

[英]@XmlEnum on attribute with JAXB

我有一个问题,用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 ...)

如果我正确理解您的需求,这应该可以解决您的问题。

再见!

UPDATE (使用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”,类型名称的定义类似于使用enumerationsimpleType

最终更新:

这就是我写的:

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"/>

这是一个带有从枚举中出来的属性的事件标记...

我希望最后一次尝试能帮到你......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM