处理JAXB中的嵌套元素 [英] Handling nested elements in JAXB
本文介绍了处理JAXB中的嵌套元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有可能让JAXB不为用作包装器的XML元素创建Java对象。例如,对于以下结构的XML
I am wondering if it is possible to have JAXB not to create Java object for XML elements that serve as wrappers. For example, for XML of the following structure
<root>
<wrapper>
<entity/>
</wrapper>
</root>
我不想要的对象< wrapper>
即将创建。所以对于类似
I do not want an object for <wrapper>
to be created at all. So for a class like
class Root {
private Entity entity;
}
< entity>
元素应该直接解组到实体字段中。
the <entity>
element should be unmarshalled directly into the entity field.
是否可以用JAXB实现?
Is it possible to achieve with JAXB?
推荐答案
虽然它需要额外的编码,但是使用瞬态包装器对象可以通过以下方式完成所需的解组:
Although it requires extra coding, the desired unmarshalling is accomplished in the following way using a transient wrapper object:
@XmlRootElement(name = "root")
public class Root {
private Entity entity;
static class Entity {
}
static class EntityWrapper {
@XmlElement(name = "entity")
private Entity entity;
public Entity getEntity() {
return entity;
}
}
@XmlElement(name = "wrapper")
private void setEntity(EntityWrapper entityWrapper) {
entity = entityWrapper.getEntity();
}
}
这篇关于处理JAXB中的嵌套元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文