我正在尝试使用 JAXB/MOXy 来映射由一组具有独特特征的接口表示的域:所有多值元素都被封装,Iterables
而不是数组或Collection
s。下面是一个演示我的案例的玩具示例。
Actor.java(注意使用Iterable<Movie>
)
package test.moxy;
public interface Actor {
public String getName();
public void setName(String name);
public Iterable<Movie> getMovies();
public void setMovies(Iterable<Movie> movies);
}
电影.java
package test.moxy;
public interface Movie {
public String getTitle();
public void setTitle(String title);
}
主班
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);
}
}
输入.xml
<?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
<?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
仅允许在集合或数组属性上,但 [movies] 不是集合或数组属性。
我尝试使用on movies 在andXMLAdapter
之间进行转换,但它似乎适用于每个元素,而不是包装器。我想知道是否以及如何指定某种包装器之类的东西?Iterable<T>
List<T>
movie
movies
XMLAdapter