Java Jackson XML 注释:具有属性的字符串元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19847094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hymanson XML Annotations: String element with attribute
提问by jtyler
I can't seem to find a way to make a Pojo Using the Hymanson-xml annotations that would generate xml like the following:
我似乎无法找到一种使用 Hymanson-xml 注释生成 Pojo 的方法,该注释会生成如下所示的 xml:
<Root>
<Element1 ns="xxx">
<Element2 ns="yyy">A String</Element2>
</Element1>
</Root>
The closest I can seem to come is the following:
我似乎最接近的是以下内容:
Root POJO:
根POJO:
public class Root {
@HymansonXmlProperty(localName = "Element1")
private Element1 element1;
public String getElement1() {
return element1;
}
public void setElement1(String element1) {
this.element1 = element1;
}
}
Element1 POJO:
元素 1 POJO:
public class Element1 {
@HymansonXmlProperty(isAttribute = true)
private String ns = "xxx";
@HymansonXmlProperty(localName = "Element2")
private Element2 element2;
public String getElement2() {
return element2;
}
public void setElement2(String element2) {
this.element2 = element2;
}
}
Element2 POJO:
元素 2 POJO:
public class Element2 {
@HymansonXmlProperty(isAttribute = true)
private String ns = "yyy";
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
But this returns back the following:
但这会返回以下内容:
<Root>
<Element1 ns="xxx">
<Element2 ns="yyy"><value>A String</value></Element2>
</Element1>
</Root>
The element tags around "A String" I do not want to display.
我不想显示“A String”周围的元素标签。
采纳答案by Ilya
You should use HymansonXmlTextannotation for value
field.
您应该对字段使用HymansonXmlText注释value
。
public class Element2
{
@HymansonXmlProperty(isAttribute = true)
private String ns = "yyy";
@HymansonXmlText
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
then XML will looks like
然后 XML 看起来像
<Root>
<Element1 ns="xxx">
<Element2 ns="yyy">A String</Element2>
</Element1>
</Root>