实施“ xml-元素”使用 MOXy

发布于 2024-10-22 19:49:11 字数 356 浏览 4 评论 0原文


我想知道如何使用 EclipseLink MOXy 实现接口类型列表,
之前,使用 JAXB ,以下注释完成了

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 {} 

支持接口类型列表的工作
???

I want to know how to implement having List of interface Type using EclipseLink MOXy,
Before, using JAXB , 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 ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2024-10-29 19:49:12

我领导 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
org.eclipse.persistence.jaxb.JAXBContext@11ddcde
<?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);

有关详细信息,请参阅:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文