首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JAXB/MOXy适应XMLElementWrapper

JAXB/MOXy适应XMLElementWrapper
EN

Stack Overflow用户
提问于 2013-10-23 13:05:49
回答 1查看 3.7K关注 0票数 1

我试图使用JAXB/MOXy映射由一组接口表示的域,这些接口具有一个独特的特性:所有多值元素都封装在Iterables中,而不是数组或Collection。下面是一个演示我的情况的玩具示例。

Iterable<Movie>**)** Actor.java (注意Actor.java的使用)

代码语言:javascript
复制
package test.moxy;

public interface Actor {

    public String getName();
    public void setName(String name);

    public Iterable<Movie> getMovies();
    public void setMovies(Iterable<Movie> movies);

}

Movie.java

代码语言:javascript
复制
package test.moxy;

public interface Movie {

    public String getTitle();
    public void setTitle(String title);

}

主类

代码语言:javascript
复制
package test.moxy;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.xml.sax.InputSource;

public class TestBinding {

    public static void main(String[] args) throws FileNotFoundException, JAXBException {

        Class<?>[] types = new Class<?>[] { Actor.class, Movie.class };

        List<String> mappings = new ArrayList<String>();
        mappings.add("test/moxy/oxm.xml");

        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, mappings);

        JAXBContext jc = JAXBContext.newInstance(types, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        InputSource is = new InputSource(new FileInputStream("src/main/java/test/moxy/input.xml"));
        Actor actor = (Actor) unmarshaller.unmarshal(is);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(actor, System.out);

    }

}

input.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<actor>
    <name>John Smith</name>
    <movies>
        <movie>
            <title>Smith's Trilogy - Part I</title>
        </movie>
        <movie>
            <title>Smith's Trilogy - Part II</title>
        </movie>
        <movie>
            <title>Smith's Trilogy - Part III</title>
        </movie>
    </movies>
</actor>

oxm.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="test.moxy" xml-mapping-metadata-complete="true">
    <java-types>
        <java-type name="Actor">
        <xml-root-element name="actor" />
            <xml-type prop-order="name movies" factory-class="test.moxy.ProxyFactory"
                factory-method="initActor" />
            <java-attributes>
                <xml-element java-attribute="name" />
                <xml-element java-attribute="movies" name="movie" >
                    <xml-element-wrapper name="movies" />
                </xml-element>
            </java-attributes>
        </java-type>
        <java-type name="Movie">
            <xml-root-element name="movie" />
            <xml-type factory-class="test.moxy.ProxyFactory"
                factory-method="initMovie" />
            <java-attributes>
                <xml-element java-attribute="title" />
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

ProxyFactory是一个标准工厂,如http://blog.bdoughan.com/2010/07/moxy-jaxb-map-interfaces-to-xml.html中的例子。

目前,我得到了以下JAXBException:异常描述:只允许在集合或数组属性上使用XmlElementWrapper,但电影不是集合或数组属性。

我尝试使用电影上的XMLAdapterIterable<T>List<T>之间进行转换,但它似乎适用于每个movie元素,而不是movies包装器。我想知道我是否以及如何为包装指定某种类型的XMLAdapter

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-23 14:14:29

正如异常声明的那样,您将无法用Iterable类型的属性映射@XmlElementWrapper

代码语言:javascript
复制
Exception in thread "main" javax.xml.bind.JAXBException: 
Exception Description: XmlElementWrapper is only allowed on a collection or array property but [movies] is not a collection or array property.
 - with linked exception:
[Exception [EclipseLink-50015] (Eclipse Persistence Services - @VERSION@.@QUALIFIER@): org.eclipse.persistence.exceptions.JAXBException
Exception Description: XmlElementWrapper is only allowed on a collection or array property but [movies] is not a collection or array property.]
    at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1068)
    at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:182)

XmlAdapter (MoviesAdapter)

相反,我们将使用XmlAdapterIterable转换为我们可以映射的东西。在本例中,我们将组成一个名为Movies的类的实例,它具有List of Movie实例。

代码语言:javascript
复制
package test.moxy;

import java.util.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MoviesAdapter extends XmlAdapter<MoviesAdapter.Movies, Iterable<Movie>> {

    public static class Movies {
        public List<Movie> movie = new ArrayList<Movie>();
    }

    @Override
    public Iterable<Movie> unmarshal(Movies v) throws Exception {
        return v.movie;
    }

    @Override
    public Movies marshal(Iterable<Movie> v) throws Exception {
        Movies movies = new Movies();
        for(Movie movie : v) {
            movies.movie.add(movie);
        }
        return movies;
    }

}

外部元数据(oxm.xml)

下面是引用oxm.xml的修改后的XmlAdapter。使用新的映射,Movies对象将导致movies元素的存在,因此我们不再需要@XmlElementWrapper元数据。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="test.moxy" xml-mapping-metadata-complete="true">
    <java-types>
        <java-type name="Actor">
        <xml-root-element name="actor" />
            <xml-type prop-order="name movies" factory-class="test.moxy.ProxyFactory"
                factory-method="initActor" />
            <java-attributes>
                <xml-element java-attribute="name" />
                <xml-element java-attribute="movies">
                    <xml-java-type-adapter value="test.moxy.MoviesAdapter"/>
                </xml-element>
            </java-attributes>
        </java-type>
        <java-type name="Movie">
            <xml-root-element name="movie" />
            <xml-type factory-class="test.moxy.ProxyFactory"
                factory-method="initMovie" />
            <java-attributes>
                <xml-element java-attribute="title" />
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19542533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档