4

我在使用 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>

我有以下代码用于 tha 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;
}

}

因此,如果您有想法,欢迎您 =)

谢谢。

4

1 回答 1

8

尝试向您的班级添加另一个字段并将其标记为@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”,并且类型名称的定义类似于simpleTypeusing enumeration

最后更新:

这是我写的:

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

那是一个事件标签,其属性来自枚举......

我希望最后一次尝试对您有所帮助...

于 2013-10-23T10:11:19.453 回答