7

我有这种情况

@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;
  // ...
}

因此,复合是简单和/或复合的列表。两者都从定义为的值扩展

public abstract class Value implements Serializable {}

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

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

Compound 不需要适配器。

问题是,如果我使用简单的“原样”,它会正确地编组/解组为

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

但是如果我在 Compound 中使用它,它会输出类似

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

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

编辑

经过几次测试,我发现问题可能出在我如何处理简单实例上。因此,我将最初的问题简化为:

给定一个简单的类

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

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

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

代替

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

?

4

2 回答 2

2

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

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

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

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

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

于 2011-05-23T21:47:19.793 回答
0

bamana 给出的答案是正确的,但是您看到的异常是由于 JAXB 参考实现中的错误造成的。此错误也存在于EclipseLink JAXB (MOXy)中,但已在 2.3.0 流中修复,可在此处获取夜间下载:

作为一种解决方法,您可以使用原始问题中的 XmlAdapter 方法:

简单适配器

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class SimpleAdapter extends XmlAdapter<String, Simple> {

    @Override
    public Simple unmarshal(String v) throws Exception {
        Simple simple = new Simple();
        simple.setSimple(v);
        return simple;
    }

    @Override
    public String marshal(Simple v) throws Exception {
        return v.getSimple();
    }

}

简单的

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

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

    public java.lang.String getSimple() {
        return simple;
    }

    public void setSimple(java.lang.String simple) {
        this.simple = simple;
    }

}

化合物

import java.util.List;

import javax.xml.bind.annotation.*;

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

    public List<Value> getValue() {
        return value;
    }

    public void setValue(List<Value> value) {
        this.value = value;
    }

}

价值

import java.io.Serializable;

public abstract class Value implements Serializable {}

演示

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Compound.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Compound compound = (Compound) unmarshaller.unmarshal(new File("input.xml"));
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(compound, System.out);
    }

}

输入.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<compound>
    <simple>
        <simple>FOO</simple>
    </simple>
    <compound/>
</compound>
于 2011-05-24T13:37:35.307 回答