我领导EclipseLink JAXB (MOXy)。我无法重现您的问题。你能提供你看到的堆栈跟踪吗?下面是我曾尝试:
演示
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.Version;
public class Demo {
public static void main(String[] args) throws Exception {
System.out.println(Version.getVersionString());
JAXBContext jc = JAXBContext.newInstance(A.class, B1.class, B2.class);
System.out.println(jc);
File xml = new File("input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<A> root = unmarshaller.unmarshal(new StreamSource(xml), A.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
jaxb.properties
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
的input.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<B1/>
<B2/>
</root>
输出
2.2.0.v20110202-r8913
[email protected]
<?xml version="1.0" encoding="UTF-8"?>
<root>
<B1/>
<B2/>
</root>
使用的EclipseLink JAXB(MOXY)的XML元数据文件
MOXY还具有延伸部,以提供所述元数据为XML文件。下面是它会是什么样子的这个例子:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="com.example">
<java-types>
<java-type name="A">
<java-attributes>
<xml-elements java-attribute="list">
<xml-element name="B1" type="com.example.B1"/>
<xml-element name="B2" type="com.example.B2"/>
</xml-elements>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
创建的JAXBContext时,该元数据文件传过来的属性:
Map<String, Object> properties = new HashMap<String, Object>(1);
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, new File("src/forum149/bindings.xml"));
JAXBContext jc = JAXBContext.newInstance(new Class[] {A.class, B1.class, B2.class}, properties);
欲了解更多信息,请参阅:
在上面提供的例子中,我能够统帅/ unmarshal通过使用XmlElements注释包含接口类型列表的对象。但我不知道如何创建一个可以做同样事情的绑定XML。 我想使用MoXy的映射文件,而不是使用注释(如在这里提到的binding.xml http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html)。假设PhoneNumber类是一个具有两个实现类的接口(例如:XPhoneNumber,YPhoneNumber),这将如何在绑定xml中进行描述? – Transient 2011-03-16 14:47:33
@ RO86 - 我已经更新了我的答案,以包含MOXy映射文件。它假定模型类位于“com.example”包中。 – 2011-03-16 17:33:40
非常感谢Blaise, – Transient 2011-03-18 09:45:22