1

Here's my intended XML structure

<Outer type="good" id="1">
  <Uid>123</Uid>
  <Name>Myself</Name>
  <Inner type="bad">This Value</Inner>
</Outer>

Here's my Object.

@XmlAccessorType(XMLAccessType.FIELD)
@XmlType(name="Outer", propOrder = {
  "uid"
  "name"
  "inner"
})
public class Outer{
  @XmlElement(name = "Uid")
  protected String uid;
  @XmlElement(name = "Name")
  protected String name;
  @XmlElement(name = "Inner")
  protected Inner inner;

  public static class Inner{
    @XmlAttribute
    private String type;
    @XmlValue
    private String value;

    //setters & getters for both
  }

  //setters & getters for all the elements
}

Now in my class I am doing

Outer o = new Outer();
o.setUid/ID/Type/Name() ; //all the setter
Inner i - new Inner();
i.setValue("This Value");
i.setType("bad");

When Irun this i am getting

If a class has @XmlElement property, it cannot have @XmlValue property.

And

Class has two properties of the same name "type" (This one is for the Source class)

And

Class has two properties of the same name "value" (This one is for Source class too)

What is happening, and what I can I do rectify this?

Thanks

2 Answers 2

2

Currently, JAXB threats both fields (due to annotations) and both pairs of get/set (due to default accessor type) as properties. So you class Inner has 4 properties.

Please, add own accessor type for Inner class

   @XmlAccessorType(XmlAccessType.FIELD)
   public static class Inner
   {  

Or annotate properties instead of fields

   public static class Inner
   {

      private String type;


      private String value;

      @XmlAttribute
      public String getType()
      {
         return type;
      }
      // setter setType

      @XmlValue
      public String getValue()
      {
         return value;
      }   
      // setter setValue
   }  
Sign up to request clarification or add additional context in comments.

Comments

0

Add XmlRootElement annotation to the Outer class, besides of that it shoud work.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Outer", propOrder = {"uid", "name", "inner"})
public static class Outer {

3 Comments

Actually, outside an Outer, there's another element, that contains the XMLRootElement. I am trying to add the Inner one to this structure, and that is causing the problem.
I see, then maybe you should post that too!
apparently, I need to annotate the static Inner class with @XmlAccessorType(XMLAccessType.FIELD) XMLType(name="",propOrder = {"value"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.