gpt4 book ai didi

java - JAXB XML 到 Java 映射

转载 作者:行者123 更新时间:2023-12-01 19:57:49 24 4
gpt4 key购买 nike

我正在尝试将以下 XML 结构映射到一个巨大的文档中。

这是文档的一部分:

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

文档中有一个 XSD 声明可以有无界的拾取、放置、goTo 和 Todo 操作数组。

我想要什么,而不是为每个元素声明一个不同的数组,例如 List、List 等。

下面是我迄今为止尝试过的:

@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 对象的问题。

理想情况下,它是一个 List<QueueItem>其中每个操作项都将用 QueueItem 包裹起来根据 Blaise Doughan 的解决方案的元素:Java XML and JSON Binding :

@XmlAccessorType(XmlAccessType.FIELD)
public class QueueItem {
@XmlAttribute
private String type;

@XmlValue
private Object value;

但是我在使用此解决方案时遇到了异常:

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。

话虽如此,但缺少您案例的背景,适合您所呈现内容的结构可能是这样的:

您的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;

}

然后 Pick、Place、GoTo 和 ToDo 类看起来类似如下:

@XmlAccessorType(XmlAccessType.FIELD)
public class Pick implements QueueItem{
@XmlValue
private String value;
}

它们都实现了相同的接口(interface),但作用不大:

public interface QueueItem { }

这将允许您对所呈现的 xml 进行编码/取消编码。

我不确定这是否适合您的情况,以及您是否可以将您的 xsd 更改为如下所示。但是您对 xsd 生成的评论 List<Object> pickOrPlaceOrGotoOrTodo让我觉得他们在创建 xsd 时也有类似的想法。

关于java - JAXB XML 到 Java 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48908986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com