我正在尝试创建一个 @XmlAdapter 以将混合列表中的单词拆分为名为 Word 的 Wrapper 类的对象。该字段还可能包含名为 Group 的 Word 扩展的实例。
这个想法是这样的:
<span>Some text and a <g>group of words</g> and more<span>
相当于:
span.getContent().add(new Word("Some");
span.getContent().add(new Word("text");
span.getContent().add(new Word("and");
span.getContent().add(new Word("a");
span.getContent().add(new Group("group of words");
span.getContent().add(new Word("and");
span.getContent().add(new Word("more");
我正在与我能想到的所有可能的 XmlMixed 和 XmlAdapter 组合进行斗争,但没有运气!
这是我的 Latets 适配器,根本不起作用:
public class SpanAdapter extends XmlAdapter<List<Word>, List> {
@Override
public List<Word> marshal(List v) throws Exception {
List<Word> list = new ArrayList<>();
for (Object o : v) {
if (o instanceof String) {
String s = (String) o;
StringTokenizer st = new StringTokenizer(s, " ");
while (st.hasMoreElements()) {
Word word = new Word();
word.setValue(st.nextToken());
list.add(word);
}
} else if (o instanceof Group) {
list.add((Group) o);
}
}
return list;
}
@Override
public List unmarshal(List<Word> v) throws Exception {
List list = new ArrayList<>();
for (Word w : v) {
if(w instanceof Group){
list.add(w);
} else{
list.add(w.getValue()+ " ");
}
}
return v;
}
}
还有我的跨度类:
@XmlRootElement(name = "span")
public class Span extends AudioComponent implements Item.Component {
private List<Word> value = new ArrayList();
@XmlMixed
@XmlElementRefs({
@XmlElementRef(type = Group.class),
@XmlElementRef(type = Word.class)
})
@XmlJavaTypeAdapter(SpanAdapter.class)
public List<Word> getValue() {
return value;
}
public void setValue(List<Word> value) {
this.value = value;
}
}
Word 和 Group 只是 String 的包装。组扩展了 Word。
我正在使用 MOXY,以防它有帮助。
提前谢谢您!
最佳答案
如果它对任何人有帮助:
我设法通过在包含 Span 元素的列表字段中设置适配器来做到这一点。
在包级别或 Span 类级别设置适配器将不起作用。
此外,适配器不是从 List 转换为 List,而是从 Span 转换为 Span,并在内部修改列表。
最终配置为:
我定义适配器的地方:
@XmlElementRefs({
@XmlElementRef(type = Span.class),
@XmlElementRef(type = Other.class),
@XmlElementRef(type = Another.class)
})
@XmlJavaTypeAdapter(value = SpanAdapter.class)
public List<Component> getComponents() {
if (components == null) components = new ArrayList();
return components;
}
适配器(无法从一个跨度到另一个跨度执行此操作,因为我在可能包含其他类的列表字段中使用它):
public class SpanAdapter extends XmlAdapter<Object, Object> {
@Override
public Object unmarshal(Object o) throws Exception {
if (o instanceof Span) {
Span span = (Span) o;
List<Span.Component> list = new ArrayList<>();
for (Object child : span.getValue()) {
if (child instanceof String) {
String s = (String) child;
for (String w : s.split(" ")) {
Word word = new Word();
word.setValue(w.trim());
list.add(word);
}
} else if (child instanceof Group) {
list.add((Group) child);
}
}
span.setValue(list);
return span;
}
return o;
}
@Override
public Object marshal(Object o) throws Exception {
if(o instanceof Span) {
Span span = (Span) o;
List list = new ArrayList<>();
Iterator<Span.Component> iterator = span.getValue().iterator();
while (iterator.hasNext()) {
Span.Component w = iterator.next();
if (w instanceof Group) {
list.add(w);
} else if (w instanceof Word) {
String value = ((Word) w).getValue();
list.add(iterator.hasNext() ? value + " " : value);
}
}
span.setValue(list);
return span;
}else return o;
}
}
关于java - JAXB @XmlMixed 和 @XmlAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22843639/