首页 > 解决方案 > 如何删除 @XmlElementRefs 并用 java 属性替换它们

问题描述

我有以下课程

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
@XmlRootElement(name = "SegmentFilter")
public class SegmentFilter {

@XmlElementRefs({
    @XmlElementRef(name = "StopRange", type = JAXBElement.class),
    @XmlElementRef(name = "OverlapRange", type = JAXBElement.class),
    @XmlElementRef(name = "DateRange", type = JAXBElement.class),
    @XmlElementRef(name = "DurationRange", type = JAXBElement.class),
    @XmlElementRef(name = "EmployeeSelector", type = JAXBElement.class),
    @XmlElementRef(name = "SegmentCode", type = JAXBElement.class),
    @XmlElementRef(name = "StartRange", type = JAXBElement.class)
})
@XmlMixed
protected List<Serializable> content;
@XmlAttribute(name = "IncludeDetailSegments")
protected Boolean includeDetailSegments;
@XmlAttribute(name = "IncludeGeneralSegments")
protected Boolean includeGeneralSegments;

public List<Serializable> getContent() {
    if (content == null) {
        content = new ArrayList<Serializable>();
    }
    return this.content;
}
public boolean isIncludeDetailSegments() {
    if (includeDetailSegments == null) {
        return true;
    } else {
        return includeDetailSegments;
    }
}

public void setIncludeDetailSegments(Boolean value) {
    this.includeDetailSegments = value;
}

public boolean isIncludeGeneralSegments() {
    if (includeGeneralSegments == null) {
        return true;
    } else {
        return includeGeneralSegments;
    }
}

public void setIncludeGeneralSegments(Boolean value) {
    this.includeGeneralSegments = value;
}

}

有人可以帮我用实际的java代码替换ref,比如......

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
@XmlRootElement(name = "SegmentFilter")
public class SegmentFilter {



public DateTimeRangeType StopRange;
public DateTimeRangeType StartRange;

//and so on with getters and setters

    @XmlAttribute(name = "IncludeDetailSegments")
protected Boolean includeDetailSegments;
@XmlAttribute(name = "IncludeGeneralSegments")
protected Boolean includeGeneralSegments;

public List<Serializable> getContent() {
    if (content == null) {
        content = new ArrayList<Serializable>();
    }
    return this.content;
}
public boolean isIncludeDetailSegments() {
    if (includeDetailSegments == null) {
        return true;
    } else {
        return includeDetailSegments;
    }
}

public void setIncludeDetailSegments(Boolean value) {
    this.includeDetailSegments = value;
}

public boolean isIncludeGeneralSegments() {
    if (includeGeneralSegments == null) {
        return true;
    } else {
        return includeGeneralSegments;
    }
}

public void setIncludeGeneralSegments(Boolean value) {
    this.includeGeneralSegments = value;
}

}

如果我使用复杂对象的实际 xsd 删除类型,我能够做到这一点,例如 DateTimeRangeType 在这种情况下,我使用 eclipse 生成 jaxb 类,这是下面的 xsd,在早期没有类型且所有元素的情况下在主父类中定义它能够获得 xsd 格式,但在 type 指向复杂类型的 xsd 的这种情况下似乎不起作用

<xs:element name="SegmentFilter">
 <xs:annotation>
<xs:documentation>
    SegmentFilter is a root-level element used 
 in a SegmentFilter request.
  </xs:documentation>
</xs:annotation>
 <xs:complexType mixed="true">
<xs:sequence>
  <xs:element name="EmployeeSelector" 
type="EmployeeSelectorType" minOccurs="0" />

    <xs:element name="DateRange" 
type="DateRangeType"/>
    <xs:element name="DurationRange" 
type="FromToTimeRangeType"/>
    <xs:element name="SegmentCode" 
type="EmptySKType"/>
    <xs:element name="OverlapRange" 
type="DateTimeRangeType"/>
    <xs:element name="StartRange" 
  type="DateTimeRangeType"/>
    <xs:element name="StopRange" 
type="DateTimeRangeType"/>

</xs:sequence>
<xs:attribute name="IncludeDetailSegments" 
default="true" type="xs:boolean"/>
<xs:attribute name="IncludeGeneralSegments" 
default="true" type="xs:boolean"/>
 </xs:complexType>
</xs:element>

标签: jaxb

解决方案


推荐阅读