Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@XmlElements marked with @XmlJavaTypeAdapters?

I have this situation

@XmlType(name ="", propOrder={"value"})
@XmlRootElement(name = "compound")
public class Compound extends Value {
  @XmlElements({
  @XmlElement(name="simple", type=Simple.class),
  @XmlElement(name="compound", type=Compound.class)
  })
  protected List<Value> value;
  // ...
}

So a Compound is a List of both Simple and/or Compound. Both extend from Value that is defined as

public abstract class Value implements Serializable {}

Simple is a class marked with an adapter to marshal/unmarshal to/from a simple string

@XmlJavaTypeAdapter(SimpleAdapter.class)
public class Simple extends Value {
  private java.lang.String simple;
  // ...
}

Compound does not need an adapter.

The problem is that if I use a Simple 'as is', it correctly marshals/unmarshals as

<simple>my.text.here</simple>

but if I use it inside a Compound it outputs something like

<compound>
  //...
  <simple>
    <value>my.text.here</value>
  </simple>
  //...
</compound>

And I'm just wondering why... Do I miss something? How can i remove that 'value'? It seems to me that the Adapter is not used at all, is it possible to use adapters in types marked inside @XmlElements?

EDIT

After few tests i found that the problem could be in how i handle a Simple instance. So I simplify my initial question in:

Given the a Simple class like

@XmlRootElement("simple")
public class Simple {
  private java.lang.String innerText;
  // getters/setters
}

how can i obtain a marshalled output like

<simple>
  my.inner.text.here
</simple>

instead of

<simple>
  <value>my.inner.text.here</value>
</simple>

?

like image 434
Andrea Boscolo Avatar asked May 21 '11 22:05

Andrea Boscolo


1 Answers

It sounds like you want private java.lang.String innerText; to be the @XmlValue of your Simple class. Try to annotate the String in Simple with the @XmlValue tag:

@XmlRootElement("simple")
public class Simple {
  @XmlValue
  private java.lang.String innerText;
  //getters/setters
}

Or if you were using annotations on your getter method (which I assume based on your XML output in the question change your @XmlElement tag to a @XmlValue tag:

@XmlValue
public java.lang.String getInnerText() {
  return innerText;
}

When I do this I get the output you are looking for in your edited question.

like image 86
bamana Avatar answered Nov 15 '22 10:11

bamana