gpt4 book ai didi

java - 具有特定类型但名称未知的 JAXB 元素

转载 作者:行者123 更新时间:2023-11-30 10:24:57 25 4
gpt4 key购买 nike

我有根 Xml document (name = "Entity")其中包含一个已知的 Xml element (name = "Header")和另一个名称未知的 Xml 元素,但已知它有一个内部 XmlElement(name="label")

以下是可能的 Xml:

<Entity>
<Header>this is a header</Header>
<a>
<label>this is element A</label>
<otherElements/>
</a>
</Entity>

<Entity>
<Header>this is a different header</Header>
<b>
<label>this is some other element of name b</label>
<others/>
</b>
</Entity>

这是我的 JAXB 注释类:

@XmlRootElement(name = "Entity")
@XmlAccessorType(XmlAccessType.NONE)
public class Entity {
@XmlElement(name = "Header")
private Header header;

@XmlElements( {
@XmlElement(name = "a", type=LabelledElement.A.class),
@XmlElement(name = "b", type=LabelledElement.B.class)
} )
private LabelledElement labelledElement;

// constructors, getters, setters...
}

@XmlAccessorType(XmlAccessType.NONE)
public abstract class LabelledElement {
@XmlElement
private String label;
@XmlAnyElement
private List<Element> otherElements;

public static class A extends LabelledElement {}
public static class B extends LabelledElement {}
}

这太棒了!但后来我注意到它不仅是 <a><b>

可能是<c> , <asd>甚至<anything> ...

因此列出 XmlElement(name = "xyz", type = LabelledElement.xyz.class)显然不是正确的解决方案。

我只关心Entity#getLabelledElement()#getLabel()不管LabelledElement名字是。

JAXB 甚至可以做到这一点吗?

最佳答案

使用 EclipseLink JAXB 实现 (MOXy),这应该可以工作:

@XmlRootElement(name = "Entity")
@XmlSeeAlso({LabelledElement.class}) //Might not be necessary
@XmlAccessorType(XmlAccessType.NONE)
public class Entity {
@XmlElement(name = "Header")
private Header header;

@XmlPath("child::*[position() = 2]")
@XmlJavaTypeAdapter(MapAdapter.class)
private Map<String,LabelledElement> labelledElementMap;


public LabelledElement getLabelledElement(){
return labelledElementMap.values().get(0);
}
// constructors, getters, setters...
}

MapAdapter 类:

public class MapAdapter extends XmlAdapter<MapAdapter.AdaptedMap, Map<String, LabelledElement>> {

public static class AdaptedMap {

@XmlVariableNode("key")
List<LabbeledElement> entries = new ArrayList<LabbeledElement>();

}

public static class AdaptedEntry {

@XmlTransient
public String key;

@XmlElement
public LabelledElement value;

}

@Override
public AdaptedMap marshal(Map<String, LabelledElement> map) throws Exception {
AdaptedMap adaptedMap = new AdaptedMap();
for(Entry<String, LabelledElement> entry : map.entrySet()) {
AdaptedEntry adaptedEntry = new AdaptedEntry();
adaptedEntry.key = entry.getKey();
adaptedEntry.value = entry.getValue();
adaptedMap.entries.add(adaptedEntry);
}
return adaptedMap;
}

@Override
public Map<String, LabelledElement> unmarshal(AdaptedMap adaptedMap) throws Exception {
List<AdaptedEntry> adaptedEntries = adaptedMap.entries;
Map<String, LabelledElement> map = new HashMap<String, LabelledElement>();
for(AdaptedEntry adaptedEntry : adaptedEntries) {
map.put(adaptedEntry.key, adaptedEntry.value);
}
return map;
}

}

作为引用,我的解决方案灵感来自 this link .

关于java - 具有特定类型但名称未知的 JAXB 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46443318/

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