1

班级:

public class OddEvenSettings {
  int oddSetting1;
  int oddSetting2;
  int evenSetting1;
  int evenSetting2;
}

所需的 XML

<OddEvenSettings>
  <odd setting1="0" setting2="0"/>
  <even setting1="0" setting2="0"/>
</OddEvenSettings>

我应该如何注释类以在序列化后获取 XML(使用 JAXB RI)?

4

2 回答 2

1

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

对于这个用例,您可以使用 MOXy 的@XmlPath扩展:

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;

@XmlRootElement("OddEvenSettings")
@XmlAccessorType(XmlAccessType.FIELD)
public class OddEvenSettings {
  @XmlPath("odd/@setting1")
  int oddSetting1;

  @XmlPath("odd/@setting2")
  int oddSetting2;

  @XmlPath("even/@setting1")
  int evenSetting1;

  @XmlPath("even/@setting2")
  int evenSetting2;
}

了解更多信息

于 2013-10-01T17:28:07.860 回答
0

我认为唯一的方法是创建 2 个新类“OddSettings”和“EvenSettings”,并将“OddEvenSettings”引用到“OddSettings”和“EvenSettings”,如下所示:

@XmlRootElement(name="OddEvenSettings")
public class OddEvenSettings {

    @XmlElement(name="odd")
    private OddSetting oddSetting = new OddSetting();

    @XmlElement(name="even")
    private EvenSetting evenSetting = new EvenSetting();

        ...
}

当然OddSettingsEvenSettings也应该注释:

@XmlAccessorType(XmlAccessType.FIELD)
public class OddSetting {
    @XmlAttribute(name="setting1")
    int oddSetting1;
    @XmlAttribute(name="setting2")
    int oddSetting2;
        ...
}

@XmlAccessorType(XmlAccessType.FIELD)
public class EvenSetting {
    @XmlAttribute(name="setting1")
    int evenSetting1;
    @XmlAttribute(name="setting2")
    int evenSetting2;
        ...
}

这会产生你需要的东西:

<OddEvenSettings>
    <odd setting1="0" setting2="0"/>
    <even setting1="0" setting2="0"/>
</OddEvenSettings>
于 2013-10-01T15:08:55.907 回答