我有以下XML:無法解析空XML屬性與屬性
<prog>
<prop1 attr="attr"> </prop1>
<prop2>some</prop2>
</prog>
我有以下類代表它:
@JsonIgnoreProperties(ignoreUnknown = true)
@JacksonXmlRootElement(localName = "prog")
public class Prog {
@JacksonXmlProperty(localName = "prop1")
private String prop1;
@JacksonXmlProperty(localName = "prop2")
private String prop2;
//getters, setters
}
但它失敗,出現以下錯誤:Could not read document: Can not instantiate value of type [simple type, class com.xxx.xxx.entities.ro.Prog] from String value ('some'); no single-String constructor/factory method\n at [Source: [email protected]; line: 18, column: 36]
下面的xml可以很好的解析(使用上面的類):
無屬性
<prog>
<prop1> </prop1>
<prop2>some</prop2>
</prog>
空PROP1(而不是空格)
<prog>
<prop1 attr="attr"></prop1>
<prop2>some</prop2>
</prog>
不是空PROP1
<prog>
<prop1 attr="attr"> g</prop1>
<prop2>some</prop2>
</prog>
是否原始的XML有效嗎?我怎麼解析它?
其實我需要跳過attr
,但我也想,作爲對象:
@JsonIgnoreProperties(ignoreUnknown = true)
public class XmlElement {
@JacksonXmlText
private String value;
//getters, setters
}
更新: 我祈求,而使用用SpringMVC控制器的分析,但它也與再現:
String xml = "<prog>" +
"<prop1 attr=\"att1\"> </prop1>" +
"<prop2>some</prop2>" +
"</prog>";
ObjectMapper xmlmapper = new XmlMapper();
Prog prog = xmlmapper.readValue(xml, Prog.class);
還顯示代碼如何調用分析器。 – Andremoniy
@Andremoniy我已更新我的問題 –