@XmlElements标有@XmlJavaTypeAdapters?

And*_*olo 7 java xml jaxb xml-serialization jaxb2

我有这种情况

@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;
  // ...
}
Run Code Online (Sandbox Code Playgroud)

因此,化合物是简单和/或化合物的列表.两者都从定义为的Value扩展

public abstract class Value implements Serializable {}
Run Code Online (Sandbox Code Playgroud)

Simple是一个标有适配器的类,用于编组/解组简单字符串

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

化合物不需要适配器.

问题是,如果我使用简单'原样',它正确编组/解组为

<simple>my.text.here</simple>
Run Code Online (Sandbox Code Playgroud)

但如果我在化合物中使用它,它会输出类似的东西

<compound>
  //...
  <simple>
    <value>my.text.here</value>
  </simple>
  //...
</compound>
Run Code Online (Sandbox Code Playgroud)

而我只是想知道为什么......我会错过什么吗?我怎样才能删除那个"价值"?在我看来,根本没有使用适配器,是否可以在@XmlElements内标记的类型中使用适配器?

编辑

经过几次测试后,我发现问题可能在于我如何处理Simple实例.所以我简化了我的初步问题:

鉴于像一个简单的类

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

我怎样才能获得像编组一样的编组输出

<simple>
  my.inner.text.here
</simple>
Run Code Online (Sandbox Code Playgroud)

代替

<simple>
  <value>my.inner.text.here</value>
</simple>
Run Code Online (Sandbox Code Playgroud)

?

小智 2

听起来您想private java.lang.String innerText;成为 Simple 类的 @XmlValue 。尝试使用 @XmlValue 标签注释 Simple 中的字符串:

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

或者,如果您在 getter 方法上使用注释(我假设基于问题中的 XML 输出,请将 @XmlElement 标记更改为 @XmlValue 标记:

@XmlValue
public java.lang.String getInnerText() {
  return innerText;
}
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我会得到您在编辑的问题中寻找的输出。