Java JAXB:我应该如何编组复杂的嵌套数据结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/818327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB: How should I marshal complex nested data structures?
提问by ivan_ivanovich_ivanoff
I have several complex data structures like
我有几个复杂的数据结构,比如
Map< A, Set< B > >
Set< Map< A, B > >
Set< Map< A, Set< B > > >
Map< A, Map< B, Set< C > > >
and so on (more complex data structures)
Note: In my case it doesn't really matter if I use Set or List.
注意:就我而言,使用 Set 还是 List 并不重要。
Now I know that JAXB let me define XmlAdapter's, that's fine, but I don't want to define an XmlAdapter for every of the given data structures (it would be just too much copy-and-paste code).
现在我知道 JAXB 让我定义XmlAdapter,这很好,但我不想为每个给定的数据结构定义一个 XmlAdapter (它只是太多的复制和粘贴代码)。
I tried to achieve my goal by declaring two generalizing XmlAdapters:
我试图通过声明两个泛化 XmlAdapter 来实现我的目标:
- one for Map:
MapAdapter<K,V>
- one for Set:
SetAdapter<V>
- 地图之一:
MapAdapter<K,V>
- 一套:
SetAdapter<V>
The problem:
JAXB complains as following:
问题:
JAXB 抱怨如下:
javax.xml.bind.JAXBException:
class java.util.Collections$UnmodifiableMap nor any of its
super class is known to this context.
Here is my adapter class:
这是我的适配器类:
import java.util.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
public class Adapters {
public final static class MapAdapter<K, V>
extends XmlAdapter<MapAdapter.Adapter<K, V>, Map<K, V>> {
@XmlType
@XmlRootElement
public final static class Adapter<K, V> {
@XmlElement
protected List<MyEntry<K, V>> key = new LinkedList<MyEntry<K, V>>();
private Adapter() {
}
public Adapter(Map<K, V> original) {
for (Map.Entry<K, V> entry : original.entrySet()) {
key.add(new MyEntry<K, V>(entry));
}
}
}
@XmlType
@XmlRootElement
public final static class MyEntry<K, V> {
@XmlElement
protected K key;
@XmlElement
protected V value;
private MyEntry() {
}
public MyEntry(Map.Entry<K, V> original) {
key = original.getKey();
value = original.getValue();
}
}
@Override
public Adapter<K, V> marshal(Map<K, V> obj) {
return new Adapter<K, V>(obj);
}
@Override
public Map<K, V> unmarshal(Adapter<K, V> obj) {
throw new UnsupportedOperationException("unmarshalling is never performed");
}
}
}
Here is my JUnit test case:
这是我的 JUnit 测试用例:
import java.io.*;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import org.junit.*;
import static java.lang.System.*;
public class SomeTest {
@Test
public void _map2()
throws Exception {
Map<String, Map<String, String>> dataStructure =
new HashMap<String, Map<String, String>>();
Map<String, String> inner1 = new HashMap<String, String>();
Map<String, String> inner2 = new HashMap<String, String>();
dataStructure.put("a", inner1);
dataStructure.put("b", inner1);
inner1.put("a1", "1");
inner1.put("a2", "2");
inner2.put("b1", "1");
inner2.put("b2", "2");
JAXBContext context = JAXBContext.newInstance(Adapters.XMap.class,
Adapters.XCount.class, Adapters.XEntry.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setAdapter(new Adapters.MapAdapter());
StringWriter sw = new StringWriter();
marshaller.marshal(dataStructure, sw);
out.println(sw.toString());
}
}
采纳答案by ivan_ivanovich_ivanoff
I've solved the problem without XmlAdapter's.
我已经解决了没有 XmlAdapter 的问题。
I've written JAXB-annotated objects for Map, Map.Entryand Collection.
The main idea is inside the method xmlizeNestedStructure(...):
我已经为Map、Map.Entry和Collection编写了 JAXB 注释的对象。
主要思想在方法xmlizeNestedStructure(...) 中:
Take a look at the code:
看一下代码:
public final class Adapters {
private Adapters() {
}
public static Class<?>[] getXmlClasses() {
return new Class<?>[]{
XMap.class, XEntry.class, XCollection.class, XCount.class
};
}
public static Object xmlizeNestedStructure(Object input) {
if (input instanceof Map<?, ?>) {
return xmlizeNestedMap((Map<?, ?>) input);
}
if (input instanceof Collection<?>) {
return xmlizeNestedCollection((Collection<?>) input);
}
return input; // non-special object, return as is
}
public static XMap<?, ?> xmlizeNestedMap(Map<?, ?> input) {
XMap<Object, Object> ret = new XMap<Object, Object>();
for (Map.Entry<?, ?> e : input.entrySet()) {
ret.add(xmlizeNestedStructure(e.getKey()),
xmlizeNestedStructure(e.getValue()));
}
return ret;
}
public static XCollection<?> xmlizeNestedCollection(Collection<?> input) {
XCollection<Object> ret = new XCollection<Object>();
for (Object entry : input) {
ret.add(xmlizeNestedStructure(entry));
}
return ret;
}
@XmlType
@XmlRootElement
public final static class XMap<K, V> {
@XmlElementWrapper(name = "map")
@XmlElement(name = "entry")
private List<XEntry<K, V>> list = new LinkedList<XEntry<K, V>>();
public XMap() {
}
public void add(K key, V value) {
list.add(new XEntry<K, V>(key, value));
}
}
@XmlType
@XmlRootElement
public final static class XEntry<K, V> {
@XmlElement
private K key;
@XmlElement
private V value;
private XEntry() {
}
public XEntry(K key, V value) {
this.key = key;
this.value = value;
}
}
@XmlType
@XmlRootElement
public final static class XCollection<V> {
@XmlElementWrapper(name = "list")
@XmlElement(name = "entry")
private List<V> list = new LinkedList<V>();
public XCollection() {
}
public void add(V obj) {
list.add(obj);
}
}
}
It works!
有用!
Let's look at a demo output:
让我们看一个演示输出:
<xMap>
<map>
<entry>
<key xsi:type="xCount" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<count>1</count>
<content xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">a</content>
</key>
<value xsi:type="xCollection" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<list>
<entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">a1</entry>
<entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">a2</entry>
<entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">a3</entry>
</list>
</value>
</entry>
<entry>
<key xsi:type="xCount" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<count>2</count>
<content xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">b</content>
</key>
<value xsi:type="xCollection" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<list>
<entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">b1</entry>
<entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">b3</entry>
<entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">b2</entry>
</list>
</value>
</entry>
<entry>
<key xsi:type="xCount" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<count>3</count>
<content xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">c</content>
</key>
<value xsi:type="xCollection" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<list>
<entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">c1</entry>
<entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">c2</entry>
<entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">c3</entry>
</list>
</value>
</entry>
</map>
</xMap>
Sorry, the demo output uses also a data structure called "count"which is not mentioned in the Adapter's source code.
抱歉,演示输出还使用 了适配器源代码中未提及的名为“count”的数据结构。
BTW:does anyone know how to remove all these annoying and (in my case) unnecessary xsi:typeattributes?
顺便说一句:有谁知道如何删除所有这些烦人的(在我的情况下)不必要的xsi:type属性?
回答by Jason S
It looks like you're on the right track with XMLAdapter... the error message may be a clue:
看起来您使用 XMLAdapter 走在正确的轨道上......错误消息可能是一个线索:
class java.util.Collections$UnmodifiableMap nor any of its super class is known to this context.
类 java.util.Collections$UnmodifiableMap 或其任何超类在此上下文中都是已知的。
are you wrapping a map using Collections.unmodifiableMap() anywhere? Where exactly does the error occur?
您是否在任何地方使用 Collections.unmodifiableMap() 包装地图?错误究竟发生在哪里?
(previous answer left as a stale record for the curious)
(以前的答案留给好奇的人作为陈旧的记录)
You can create custom marshaller/unmarshaller logic that works a little more straighforward than the Adapters idea (I think; I haven't used that one before).
您可以创建自定义的编组器/解组器逻辑,它比适配器的想法更直接(我认为;我以前没有使用过那个)。
Basically the idea is that you specify a static function to do the work, and you can also create a custom class. (I usually put the static function in the class in question, but you don't have to.) Then you put a line in your .XJB file to tell JAXB to use your static function.
基本上这个想法是你指定一个静态函数来完成这项工作,你也可以创建一个自定义类。(我通常将静态函数放在有问题的类中,但您不必这样做。)然后在 .XJB 文件中放置一行以告诉 JAXB 使用您的静态函数。
Now that I took a look at my existing code, I see that all I was doing was converting an attribute string to a custom Java object. Here's the code, for reference, but it's just for attributes.
现在我查看了现有代码,我发现我所做的只是将属性字符串转换为自定义 Java 对象。这是代码,仅供参考,但仅用于属性。
JAXB file:
JAXB 文件:
<?xml version="1.0" ?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
jaxb:version="2.0">
<jaxb:bindings schemaLocation={your schema} node="/xsd:schema">
<jaxb:bindings node={some XPATH expression to select a node}>
<jaxb:bindings node={maybe another XPATH relative to the above}>
<jaxb:property>
<jaxb:baseType>
<jaxb:javaType name={your custom Java class}
parseMethod={your static method for unmarshaling}
printMethod={your static method for marshaling}
/>
</jaxb:baseType>
</jaxb:property>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
(parseMethod and printMethod convert to/from attribute strings)
(parseMethod 和 printMethod 转换为/从属性字符串)
回答by leef
Here is my marshaller/unmarshaller for the list of @XmlType class.
这是@XmlType 类列表的编组器/解组器。
E.g
例如
//Type to marshall
@XmlType(name = "TimecardForm", propOrder = {
"trackId",
"formId"
})
public class TimecardForm {
protected long trackId;
protected long formId;
...
}
//a list holder
@XmlRootElement
public class ListHodler<T> {
@XmlElement
private List<T> value ;
public ListHodler() {
}
public ListHodler(List<T> value) {
this.value = value;
}
public List<T> getValue() {
if(value == null)
value = new ArrayList<T>();
return this.value;
}
}
//marshall collection of T
public static <T> void marshallXmlTypeCollection(List<T> value,
Class<T> clzz, OutputStream os) {
try {
ListHodler<T> holder = new ListHodler<T>(value);
JAXBContext context = JAXBContext.newInstance(clzz,
ListHodler.class);
Marshaller m = context.createMarshaller();
m.setProperty("jaxb.formatted.output", true);
m.marshal(holder, os);
} catch (JAXBException e) {
e.printStackTrace();
}
}
//unmarshall collection of T
@SuppressWarnings("unchecked")
public static <T> List<T> unmarshallXmlTypeCollection(Class<T> clzz,
InputStream input) {
try {
JAXBContext context = JAXBContext.newInstance(ListHodler.class, clzz);
Unmarshaller u = context.createUnmarshaller();
ListHodler<T> holder = (ListHodler<T>) u.unmarshal(new StreamSource(input));
return holder.getValue();
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
回答by Henry
Following is the code with "dexmlize" ability based on above Ivan's code. Usage:
以下是基于上述 Ivan 代码的具有“dexmlize”能力的代码。用法:
Map<String, List> nameMapResult = (Map<String, List>) Adapters.dexmlizeNestedStructure(unmarshallResult);
In order to restore the collection and map class, a new field will be xmlized to record the class information. Detailed code:
为了恢复集合和映射类,会xml化一个新的字段来记录类信息。详细代码:
class Adapters {
private Adapters() {
}
public static Class<?>[] getXmlClasses() {
return new Class<?>[]{XMap.class, XEntry.class, XCollection.class};
}
public static Object xmlizeNestedStructure(Object input) {
if (input instanceof Map<?, ?>) {
return xmlizeNestedMap((Map<?, ?>) input);
}
if (input instanceof Collection<?>) {
return xmlizeNestedCollection((Collection<?>) input);
}
return input; // non-special object, return as is
}
public static Object dexmlizeNestedStructure(Object input) {
if (input instanceof XMap<?, ?>) {
return dexmlizeNestedMap((XMap<?, ?>) input);
}
if (input instanceof XCollection<?>) {
return dexmlizeNestedCollection((XCollection<?>) input);
}
return input; // non-special object, return as is
}
private static Object dexmlizeNestedCollection(XCollection<?> input)
{
Class<? extends Collection> clazz = input.getClazz();
Collection collection = null;
try
{
collection = clazz.newInstance();
List dataList = input.getList();
for (Object object : dataList)
{
collection.add(dexmlizeNestedStructure(object));
}
}
catch (Exception e)
{
e.printStackTrace();
}
return collection;
}
private static Object dexmlizeNestedMap(XMap<?, ?> input)
{
Class<? extends Map> clazz = input.getClazz();
Map map = null;
try
{
map = clazz.newInstance();
List<? extends XEntry> entryList = input.getList();
for (XEntry xEntry : entryList)
{
Object key = dexmlizeNestedStructure(xEntry.getKey());
Object value = dexmlizeNestedStructure(xEntry.getValue());
map.put(key, value);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return map;
}
public static XMap<?, ?> xmlizeNestedMap(Map<?, ?> input) {
XMap<Object, Object> ret = new XMap<Object, Object>(input.getClass());
for (Map.Entry<?, ?> e : input.entrySet()) {
ret.add(xmlizeNestedStructure(e.getKey()),
xmlizeNestedStructure(e.getValue()));
}
return ret;
}
public static XCollection<?> xmlizeNestedCollection(Collection<?> input) {
XCollection<Object> ret = new XCollection<Object>(input.getClass());
for (Object entry : input) {
ret.add(xmlizeNestedStructure(entry));
}
return ret;
}
@XmlType
@XmlRootElement
public final static class XMap<K, V>{
private List<XEntry<K, V>> list = new ArrayList<XEntry<K, V>>();
private Class<? extends Map> clazz = null;
public XMap(Class mapClazz) {
this.clazz = (Class<? extends Map>)mapClazz;
}
public XMap() {
}
public void add(K key, V value) {
list.add(new XEntry<K, V>(key, value));
}
@XmlElementWrapper(name = "map")
@XmlElement(name = "entry")
public List<XEntry<K, V>> getList()
{
return list;
}
public void setList(List<XEntry<K, V>> list)
{
this.list = list;
}
@XmlElement(name="clazz")
public Class<? extends Map> getClazz()
{
return clazz;
}
public void setClazz(Class<? extends Map> clazz)
{
this.clazz = clazz;
}
}
@XmlType
@XmlRootElement
public final static class XEntry<K, V> {
private K key;
private V value;
private XEntry() {
}
public XEntry(K key, V value) {
this.key = key;
this.value = value;
}
@XmlElement
public K getKey()
{
return key;
}
public void setKey(K key)
{
this.key = key;
}
@XmlElement
public V getValue()
{
return value;
}
public void setValue(V value)
{
this.value = value;
}
}
@XmlType
@XmlRootElement
public final static class XCollection<V> {
private List<V> list = new ArrayList<V>();
private Class<? extends Collection> clazz = null;
public XCollection(Class collectionClazz) {
this.clazz = collectionClazz;
}
public XCollection() {
}
public void add(V obj) {
list.add(obj);
}
@XmlElementWrapper(name = "collection")
@XmlElement(name = "entry")
public List<V> getList()
{
return list;
}
public void setList(List<V> list)
{
this.list = list;
}
@XmlElement(name="clazz")
public Class<? extends Collection> getClazz()
{
return clazz;
}
public void setClazz(Class<? extends Collection> clazz)
{
this.clazz = clazz;
}
}
}
回答by Hunaid Husain
I had the same requirement to use a Map< String,Map< String,Integer>>. I used the XMLAdapter and it worked fine. Using XMLAdaptor is the cleanest solution I think. Below is the code of the adaptor. This is the jaXb class code snippet.
我对使用 Map< String,Map< String,Integer>> 也有同样的要求。我使用了 XMLAdapter,它运行良好。使用 XMLAdaptor 是我认为最干净的解决方案。下面是适配器的代码。这是 jaXb 类代码片段。
@XmlJavaTypeAdapter(MapAdapter.class)
Map<String, Map<String, Integer>> mapOfMap = new HashMap<String,Map<String, Integer>>();
MapType Class :
地图类型类:
public class MapType {
public List<MapEntryType> host = new ArrayList<MapEntryType>();
}
MapEntry Type Class:
MapEntry 类型类:
public class MapEntryType {
@XmlAttribute
public String ip;
@XmlElement
public List<LinkCountMapType> request_limit = new ArrayList<LinkCountMapType>();
}
LinkCountMapType Class:
LinkCountMapType 类:
public class LinkCountMapType {
@XmlAttribute
public String service;
@XmlValue
public Integer count;
}
Finally the MapAdaptor Class:
最后是 MapAdaptor 类:
public final class MapAdapter extends XmlAdapter<MapType, Map<String, Map<String, Integer>>> {
@Override
public Map<String, Map<String, Integer>> unmarshal(MapType v) throws Exception {
Map<String, Map<String, Integer>> mainMap = new HashMap<String, Map<String, Integer>>();
List<MapEntryType> myMapEntryTypes = v.host;
for (MapEntryType myMapEntryType : myMapEntryTypes) {
Map<String, Integer> linkCountMap = new HashMap<String, Integer>();
for (LinkCountMapType myLinkCountMapType : myMapEntryType.request_limit) {
linkCountMap.put(myLinkCountMapType.service, myLinkCountMapType.count);
}
mainMap.put(myMapEntryType.ip, linkCountMap);
}
return mainMap;
}
@Override
public MapType marshal(Map<String, Map<String, Integer>> v) throws Exception {
MapType myMapType = new MapType();
List<MapEntryType> entry = new ArrayList<MapEntryType>();
for (String ip : v.keySet()) {
MapEntryType myMapEntryType = new MapEntryType();
Map<String, Integer> linkCountMap = v.get(ip);
List<LinkCountMapType> linkCountList = new ArrayList<LinkCountMapType>();
for (String link : linkCountMap.keySet()) {
LinkCountMapType myLinkCountMapType = new LinkCountMapType();
Integer count = linkCountMap.get(link);
myLinkCountMapType.count = count;
myLinkCountMapType.service = link;
linkCountList.add(myLinkCountMapType);
}
myMapEntryType.ip = ip;
myMapEntryType.request_limit = linkCountList;
entry.add(myMapEntryType);
}
myMapType.host = entry;
return myMapType;
}
}
}
Marshalling a Jaxb Object will give the below XML
编组 Jaxb 对象将提供以下 XML
<mapOfmap>
<host ip="127.0.0.1">
<request_limit service="service1">7</request_limit>
<request_limit service="service2">8</request_limit>
</host>
</mapOfmap>
回答by user841942
When working with complex schema structures - the JAXB binding can be crucial in resolving conflicted objects. The CAM Editor tool on Sourceforge allows you to automatically create JAXB bindings - see the quick guide here for more details - http://www.cameditor.org/#JAXB_Bindings
在处理复杂的模式结构时 - JAXB 绑定对于解决冲突对象至关重要。Sourceforge 上的 CAM 编辑器工具允许您自动创建 JAXB 绑定 - 有关更多详细信息,请参阅此处的快速指南 - http://www.cameditor.org/#JAXB_Bindings
回答by Chris
To fix this for JSON do: Hymanson with jaxb
要为 JSON 解决此问题,请执行以下操作: Hymanson with jaxb
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>