您不需要做任何特别的事情:
乙
我已B
根据您之前的一个问题修改了该类以填充该x
属性:
package forum8739246;
public class B extends A {
private Foo x;
public B() {
x = new Foo();
}
public Foo getX() {
return this.x;
}
}
oxm.xml
以下是我基于您对我的原始答案的评论的元数据文件。
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
version="2.3"
package-name="forum8739246">
<java-types>
<java-type name="B" xml-accessor-type="FIELD">
<java-attributes>
<xml-element java-attribute="x" name="X"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
演示
package forum8739246;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.*;
import javax.xml.namespace.QName;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8739246/oxm.xml");
JAXBContext jc = JAXBContext.newInstance(new Class[] {C.class},properties);
System.out.println(jc.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<B> b = new JAXBElement<B>(new QName("b"), B.class, new B());
marshaller.marshal(b, System.out);
JAXBElement<C> c = new JAXBElement<C>(new QName("c"), C.class, new C());
marshaller.marshal(c, System.out);
}
}
输出
从输出中可以看出,x 属性被编组用于B
和的两个实例C
:
class org.eclipse.persistence.jaxb.JAXBContext
<?xml version="1.0" encoding="UTF-8"?>
<b>
<X/>
</b>
<?xml version="1.0" encoding="UTF-8"?>
<c>
<X/>
</c>