如何一起使用@ XmlMixed,@ XMLElement和@XmlValue?

时间:2013-11-18 07:48:30

标签: java xml jaxb unmarshalling

我已经看过一些与此相关的讨论,但不能在以下场景中应用此方法。我正在尝试unmarshall an xml using jaxb。 以下是我所有课程的层次结构。

 @XmlRootElement(name="entry")
 class Entry extedns Base{

     @XmlElement
     private String id;
     @XmlElement
     private String name;
     @XmlElementRef
     @XmlMixed
     private Begin begin;
     @XmlElementRef
     @XmlMixed
     private End end;
     @XmlElementRef
     private List<Link> links;


//Getter setters
}



@XmlRootElement(name="begin",namespace = "something")
    public class Begin extends AnotherBase{
            @XmlValue
            private Float mFloat;
            //Getter Setters
    }

@XmlRootElement(name="end",namespace = "something")
public class End  extends AnotherBase{
        @XmlValue
        private Float mFloat;
        //Getter Setters
}


@XmlRootElement(name="link",namespace = "something")
public class Link extends Base{
        private String attname;
        private String attValue;
        //Getter Setters
}

我想要一个像下面这样的xml:

<entry>
    <id>...</id>
    <name>...</name>
    <begin>1.23344</begin>
    <end>5.0</end>
    <link>
        <link>......</link>
        <link>......</link>
    </link>
</entry>

尽你所能,

  1. Entry类包含2个@XmlElement带注释的变量和2个@XmlElementRef带注释的变量(Begin,End)。

  2. Begin和End类都有@Xmlvalue注释变量。

  3. Begin和End类都来自其他类

  4. Link类可能包含属性和元素类型的变量。

  5. 我没有使用@XmlMixed,因为您可以看到我将@XmlMixed放在Entry类的变量声明的顶部。

    此编组的结果是

    @XmlValue is not allowed on a class that derives another class.
        this problem is related to the following location:
            at private java.lang.Float Begin.mFloat
            at Begin
            at private Begin Entry.begin
    
    If a class has @XmlElement property, it cannot have @XmlValue property.
     this problem is related to the following location:
        at private java.lang.Float Begin.mFloat
        at Begin
    

1 个答案:

答案 0 :(得分:1)

您可以使用AnotherBase注释@XmlTransient,将其作为映射的超类删除。如果AnotherBase上有任何未使用@XmlAttribute映射的获取/设置方法对,则需要使用@XmlTransient对其进行注释。

@XmlMixedbegin属性不需要end注释。

我注意到你正在注释字段(实例变量)。执行此操作时,应在类@XmlAccessorType(XmlAccessType.FIELD)上指定以下内容`