JAXB XML到Java的映射 [英] JAXB XML To Java Mapping
问题描述
我正在尝试将以下XML结构映射到一个巨大的文档中.
I'm trying to map the following XML structure to within a huge document.
这是文档的一部分:
<operationQueue>
<pick>...</pick>
<place>...</place>
<goTo>...</goTo>
<toDo>...</toDo>
...
<pick>...</pick>
<place>...</place>
<goTo>...</goTo>
<toDo>...</toDo>
</operationQueue>
该文档有一个XSD,用于声明可以有无限制的选择,放置,goTo和Todo操作数组.
There is an XSD for the document declares that there can be unbounded array of pick, place, goTo and Todo operations.
我想要而不是为每个元素(例如List,List等)声明不同的数组.
What I would like rather than declare a different array for each element, like List, List etc.
以下,到目前为止我已经尝试过:
Below, what I have tried so far:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "operationQueue", propOrder = {
"pick", "place", "goTo", "toDo"
})
public class OperationQueue {
@XmlPath("operationQueue/pick")
@XmlElement(name = "pick")
public List<Pick> pick;
@XmlPath("operationQueue/place")
@XmlElement(name = "place")
public List<Place> place;
@XmlPath("operationQueue/goTo")
@XmlElement(name = "goTo")
public List<GoTo> goTo;
@XmlPath("operationQueue/toDo")
@XmlElement(name = "toDo")
public List<oDo> toDo;
...
}
这会导致封送和解组问题,并使JSON转换的操作复杂化.我试图创建一个元素QueueItem,每个可操作的Item都对其进行了增强,但是我面临着从JSON问题到Java对象的反序列化.
That causes problems with marshaling and unmarshalling and complicates operations with JSON conversions. I`ve tried to create a single element QueueItem and every operational Item enhanced it, but I faced with a deserializing from JSON problem to Java object.
理想地,它将是单个 List< QueueItem>
,根据Blaise Doughan的解决方案,每个操作项都将用 QueueItem
元素包装: Java XML和JSON绑定:>
Ideally, it would be a single List<QueueItem>
where every operation item would be wrapped with QueueItem
element according to Blaise Doughan`s solution: Java XML and JSON Binding:
@XmlAccessorType(XmlAccessType.FIELD)
public class QueueItem {
@XmlAttribute
private String type;
@XmlValue
private Object value;
但是我在此解决方案上遇到了一个例外:
But I`ve faced an exception with this solution:
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:152)
at com.sun.xml.internal.bind.v2.runtime.property.ValueProperty.<init>(ValueProperty.java:66)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:95)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:166)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:488)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:507)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:90)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:166)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:488)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:507)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.<init>(ArrayElementProperty.java:97)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.<init>(ArrayElementNodeProperty.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:166)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:488)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:507)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:90)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:166)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:488)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:305)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:124)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1123)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:147)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:462)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at com.tideworks.tug.ecs.Main1.main(Main1.java:57)
您对实现单个
推荐答案
我同意daniu的观点,即通常应该使用xsd.
I agree with daniu that in general you should use the xsd.
话虽如此,但缺少您的案情背景,可以与您提出的内容一起使用的结构可能是这样的:
Having said that and missing context of your case, a structure that would work with what you present can be this:
您的OperationQueue类:
Your OperationQueue class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class OperationQueue {
@XmlElements({
@XmlElement(name = "pick", type = Pick.class),
@XmlElement(name = "place", type = Place.class),
@XmlElement(name = "goTo", type = GoTo.class),
@XmlElement(name = "toDo", type = ToDo.class),
})
public List<QueueItem> item;
}
,然后是Pick,Place,GoTo和ToDo类,如下所示:
and then Pick, Place, GoTo and ToDo classes look similar like the following:
@XmlAccessorType(XmlAccessType.FIELD)
public class Pick implements QueueItem{
@XmlValue
private String value;
}
它们都实现了相同的接口,作用不大:
They all implement the same interface which doesnt do much:
public interface QueueItem { }
这将使您能够从显示的xml进行封送/封送.
This will allow you to marshal/unmarshal to/from the presented xml.
我不确定这是否适合您的情况,以及您是否可以更改xsd使其看起来像这样.但是您对生成xcode的 List< Object>的评论不完整.pickOrPlaceOrGotoOrTodo
让我觉得他们在创建xsd时也有类似的想法.
I am not sure if it is good for your case and if you can change your xsd to look something like this. But your comment about the xsd generating List<Object> pickOrPlaceOrGotoOrTodo
makes me think they had a similar thought when creating the xsd.
这篇关于JAXB XML到Java的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!