简体   繁体   English

JAXB XML到Java的映射

[英]JAXB XML To Java Mapping

I'm trying to map the following XML structure to within a huge document. 我正在尝试将以下XML结构映射到一个大型文档中。

Here is a piece of the document: 这是文档的一部分:

<operationQueue>
    <pick>...</pick>
    <place>...</place>
    <goTo>...</goTo>
    <toDo>...</toDo>
    ...
    <pick>...</pick>
    <place>...</place>
    <goTo>...</goTo>
    <toDo>...</toDo>    
</operationQueue>

There is an XSD for the document declares that there can be unbounded array of pick, place, goTo and Todo operations. 该文档有一个XSD,它声明可以存在无限制的pick,place,goTo和Todo操作数组。

What I would like rather than declare a different array for each element, like List, List etc. 我想要而不是为每个元素(如List,List等)声明不同的数组。

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;
...
}

That causes problems with marshaling and unmarshalling and complicates operations with JSON conversions. 这会导致封送和解组问题,并使JSON转换的操作复杂化。 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. 我试图创建单个元素QueueItem,每个可操作的Item都对其进行了增强,但是我面临着从JSON问题到Java对象的反序列化。

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 : 理想情况下,它将是单个List<QueueItem> ,根据Blaise Doughan的解决方案: Java XML和JSON绑定 ,每个操作项都将被QueueItem元素包装

@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)

Do you have any ideas how to implement a single 您有任何想法如何实施一个

I agree with daniu that in general you should use the xsd. 我同意daniu的意见,一般来说,您应该使用xsd。

Having said that and missing context of your case, a structure that would work with what you present can be this: 话虽如此,但您的案件缺少相关内容,因此可以与您提出的内容一起使用的结构可以是:

Your OperationQueue class: 您的OperationQueue类:

@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;

}

and then Pick, Place, GoTo and ToDo classes look similar like the following: 然后Pick,Place,GoTo和ToDo类看起来类似于以下内容:

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

This will allow you to marshal/unmarshal to/from the presented xml. 这将使您可以从提供的xml封送/解组。

I am not sure if it is good for your case and if you can change your xsd to look something like this. 我不确定这是否适合您的情况,以及您是否可以更改xsd使其看起来像这样。 But your comment about the xsd generating List<Object> pickOrPlaceOrGotoOrTodo makes me think they had a similar thought when creating the xsd. 但是您对生成xsd的List<Object> pickOrPlaceOrGotoOrTodo使我认为他们在创建xsd时也有类似的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM