web2dev
web2dev

Reputation: 557

How to use @XmlMixed , @XmlElement and @XmlValue all together?

I have already seen some of the discussions related to this but cannot apply this in following scenario . I am trying to unmarshall an xml using jaxb. Following is the hierarchy of all my classes .

 @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
}

I want an xml like following :

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

As you can that ,

  1. The Entry class has both 2 @XmlElement annotated variable and 2 @XmlElementRef annoted Variable (Begin,End) .

  2. Both Begin and End class has @Xmlvalue annotated variable .

  3. Both Begin and End class have derived from some other class

  4. The Link class might have Variables of type attributes and elements.

I failed to use @XmlMixed , as you can see i put @XmlMixed at the top of the variable declaration in Entry class .

The result of this marshalling is

@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

Upvotes: 3

Views: 3273

Answers (1)

bdoughan
bdoughan

Reputation: 148977

You can annotate AnotherBase with @XmlTransient to remove it as a mapped super class. If there are any get/set method pairs on AnotherBase that are not mapped with @XmlAttribute then you will need to annotate them with @XmlTransient.

You don't need the @XmlMixed annotations on the begin and end properties.

I notice you are annotating the fields (instance variables). When you do this you should specify the following on your class @XmlAccessorType(XmlAccessType.FIELD)`

Upvotes: 1

Related Questions