开发者

Implement " xml-elements" using MOXy

开发者 https://www.devze.com 2023-02-17 11:12 出处:网络
I want to know how to implement having List of interface Type using EclipseLink MOXy, Before, using JA开发者_StackOverflow社区XB , The following annotations did the job

I want to know how to implement having List of interface Type using EclipseLink MOXy,

Before, using JA开发者_StackOverflow社区XB , The following annotations did the job

class A {

   @XmlElements({
       @XmlElement(name = "B1", type = B1.class),
       @XmlElement(name = "B2", type = B2.class)
    })
    List< B > list;

}

interface B{}

class B1 implements B {}

class B2 implements B {} 

to support List of interface type ???


I lead EclipseLink JAXB (MOXy). I am not able to reproduce your issue. Could you provide the stack trace you are seeing? The following is what I have tried:

Demo

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>

Output

2.2.0.v20110202-r8913
org.eclipse.persistence.jaxb.JAXBContext@11ddcde
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <B1/>
   <B2/>
</root>

Using EclipseLink JAXB (MOXy)'s XML Metadata File

MOXy also has an extension to provide the metadata as an XML file. Below is what it would look like for this example:

<?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>

This metadata file is passed in as a property when the JAXBContext is created:

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);

For more information see:

  • http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html
0

精彩评论

暂无评论...
验证码 换一张
取 消