我试图用Jackson xml在POJO中反序列化那个奇怪的(格式错误的?)xml:
<module id="1">
<field id="1">
<title>test</title>
<value>test value</value>
</field>
<field id="6">
<title>test2</title>
<value>first value</value>
<value checked="false">second value</value>
</field>
</module>
这是我到目前为止使用的模型:
public class Field{
@JacksonXmlProperty(isAttribute = true)
private String id;
@JacksonXmlProperty(localName = "title")
private String title;
@JsonDeserialize(as=Value.class)
private Value value;
}
public class Value{
@JacksonXmlProperty(isAttribute = true)
private String checked;
@JacksonXmlText(value = true)
private String value;
}
到目前为止,除了“value”标签之外,我几乎可以阅读所有内容。 如果只有一个“value”标签,我可以拥有它,但是一旦有多个“value”标签,我只能得到最后一个!当然,我需要得到一切。
我也尝试过放置一个Value类数组,但看起来它已经崩溃了。
任何帮助都将非常感谢^^ 谢谢!
答案 0 :(得分:0)
将值作为列表或数组,并使用@JacksonXmlElementWrapper
注释。试试这个。
import java.io.File;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
@JacksonXmlRootElement
class Module {
@JacksonXmlProperty(isAttribute = true)
private String id;
@JacksonXmlProperty(localName = "field")
@JacksonXmlElementWrapper(useWrapping=false)
List<Field> field;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Field> getField() {
return field;
}
public void setField(List<Field> field) {
this.field = field;
}
@Override
public String toString() {
return "Module [id=" + id + ", " + field + "]";
}
}
class Field {
@JacksonXmlProperty(isAttribute = true)
private String id;
@JacksonXmlProperty(localName = "title")
private String title;
@JacksonXmlProperty(localName = "value")
@JacksonXmlElementWrapper(useWrapping=false)
private List<Value> value;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Value> getValue() {
return value;
}
public void setValue(List<Value> value) {
this.value = value;
}
@Override
public String toString() {
return "Field [id=" + id + ", title=" + title + ", value=" + value
+ "]";
}
}
class Value{
@JacksonXmlProperty(isAttribute = true)
private String checked;
@JacksonXmlText(value = true)
private String value;
public String getChecked() {
return checked;
}
public void setChecked(String checked) {
this.checked = checked;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Value [checked=" + checked + ", " + value + "]";
}
}
public class JacksonXmlParser {
public static void main(String[] args) throws Exception {
ObjectMapper xmlMapper = new XmlMapper();
Module obj = xmlMapper.readValue(new File("module.xml"), Module.class);
System.out.println(obj);
}
}
答案 1 :(得分:-1)
你的类字段存在缺陷,它应该包含一个Value列表而不是一个值,替换
<html>
<body>
<table class="calc" cellpadding=2>
<tr>
<td><input type="button" class="calc" id="screen1" value="0"></td>
<td><input type="button" class="calc" id="screen2" value="0"></td>
<td><input type="button" class="calc" id="screen3" value="0"></td>
</tr>
</table>
<table class="calc" cellpadding=2>
<tr>
<td><button type="button" onclick="pushButton(1)" value="Call2Functions">1</td>
<td><button type="button" onclick="pushButton(2)" value="Call2Functions">2</td>
<td><button type="button" onclick="pushButton(3)" value="Call2Functions">3</td>
</tr>
<tr>
<td><input type="radio" id="Check1" name="radio" onclick="check(1)">button1</td>
<td><input type="radio" id="Check2" name="radio" onclick="check(2)">button2</td>
<td><input type="radio" id="Check3" name="radio" onclick="check(3)">button3</td>
</tr>
</table>
<script>
function check(value){
if(document.getElementById("Check"+value).checked)
{
buttonValue = value;
}
}
function pushButton(addValue) {
if (buttonValue == 'C') {
document.getElementById('screen').value = '0';
}
else {//this is where most changes occured
var x= document.getElementById('screen'+buttonValue).value
x =parseInt(x)+ parseInt(addValue);
document.getElementById('screen'+buttonValue).value=x;
localStorage.setItem('answer', x);
}
}
function calculate(equation) {
var answer = eval(equation);
document.getElementById('screen'+buttonValue).value = answer;
}
</script>
</body>
</html>
带
private Value value;
并且在您的xml中,您有一个名为private List<Value> values;
的标记,而在字段中存在与<title>..</title>
不匹配的标记
juste replace
titre
带
@JacksonXmlProperty(localName = "titre")