A smart person somewhere has designed an xml like this:
一个聪明的人在某处设计了一个像这样的xml:
<node>
<subnode value="sv1"/>
<anotherSubNode value="asv" />
</node>
I would like to map this to POJO object looking like this:
我想将此映射到POJO对象,如下所示:
class Node {
String subnode;
String anotherSubNode;
//...
}
Instead of
class NodeValue {
@XmlAttribute
String value;
}
class Node {
NodeValue subnode;
NodeValue anotherSubNode;
/...
}
Is there a way I can efficiently do this with standard java xml marshaling? Or even non-standard?
有没有办法用标准的java xml编组有效地做到这一点?甚至不标准?
1 个解决方案
#1
0
An XmlAdapter is the simplest and most generic solution. An adapter for String field would look like that:
XmlAdapter是最简单,最通用的解决方案。 String字段的适配器看起来像这样:
public class NodeValueStringAttrMarshaller extends XmlAdapter<NodeValueElement, String> {
@Override
public NodeValueElement marshal(String v) throws Exception {
return new NodeValueElement(v);
}
@Override
public String unmarshal(NodeValueElement v) throws Exception {
if (v==null) return "";
return v.getValue();
}
}
If you have fields of other types, like Long or Boolean, you would have to implement other (equally simple) adapters.
如果您有其他类型的字段,如Long或Boolean,则必须实现其他(同样简单的)适配器。
A generic field-object to marshal to will also be necessary:
要编组的通用字段对象也是必要的:
@XmlAccessorType(XmlAccessType.FIELD)
public class NodeValueElement {
@XmlAttribute(name="value")
String value;
public NodeValueElement() {
}
public NodeValueElement(String value) {
super();
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
To use the adapter you have to annotate appropriate fields in your POJO:
要使用适配器,您必须在POJO中注释相应的字段:
class Node {
@XmlJavaTypeAdapter(value=NodeValueStringAttrMarshaller.class)
String subnode;
@XmlJavaTypeAdapter(value=NodeValueLongAttrMarshaller.class)
Long anotherSubNode;
//...
}
#1
0
An XmlAdapter is the simplest and most generic solution. An adapter for String field would look like that:
XmlAdapter是最简单,最通用的解决方案。 String字段的适配器看起来像这样:
public class NodeValueStringAttrMarshaller extends XmlAdapter<NodeValueElement, String> {
@Override
public NodeValueElement marshal(String v) throws Exception {
return new NodeValueElement(v);
}
@Override
public String unmarshal(NodeValueElement v) throws Exception {
if (v==null) return "";
return v.getValue();
}
}
If you have fields of other types, like Long or Boolean, you would have to implement other (equally simple) adapters.
如果您有其他类型的字段,如Long或Boolean,则必须实现其他(同样简单的)适配器。
A generic field-object to marshal to will also be necessary:
要编组的通用字段对象也是必要的:
@XmlAccessorType(XmlAccessType.FIELD)
public class NodeValueElement {
@XmlAttribute(name="value")
String value;
public NodeValueElement() {
}
public NodeValueElement(String value) {
super();
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
To use the adapter you have to annotate appropriate fields in your POJO:
要使用适配器,您必须在POJO中注释相应的字段:
class Node {
@XmlJavaTypeAdapter(value=NodeValueStringAttrMarshaller.class)
String subnode;
@XmlJavaTypeAdapter(value=NodeValueLongAttrMarshaller.class)
Long anotherSubNode;
//...
}