如何使用Jackson将带有重复XML标记的XML解析为POJO? [英] How to parse XML with repeated XML tags into a POJO using Jackson?
本文介绍了如何使用Jackson将带有重复XML标记的XML解析为POJO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到使用Jackson将此XML解析为POJO的问题。我已经阅读了有关将类反序列化为POJOS的类的所有前面的描述,但我一直得到Null指针或者不是END元素警告。我非常困惑,非常感谢任何帮助。
I am having issues with parsing this XML into a POJO using Jackson. I have read all the previous descriptions on making classes to de-serialise the XML into POJOS but I keep getting either Null pointers or not END of element warnings. I am extremely confused and any help is much appreciated.
输入xml是
<row>
<entry align="right" valign="top">20</entry>
<entry align="right" valign="top">1A</entry>
<entry valign="top">SData</entry>
<entry align="center" valign="top">2</entry>
<entry valign="top">binary</entry>
<entry valign="top">Java enterprise</entry>
</row>
我使用的代码是;
static void testSmallXml(){
String big = null;
try
{
big = readFileToString("other/testXML/NewFile.xml");
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
XmlMapper xmlMapper = new XmlMapper();
String small = big.substring(big.lastIndexOf("<row>"), big.lastIndexOf("</row>")+8);
try
{
rows in = xmlMapper.readValue(small, rows.class);
System.out.println(in.entries[0].value);
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(small);
}
我的POJO课程
@JacksonXmlRootElement(localName = "row")
@JsonIgnoreProperties(ignoreUnknown = true)
public static class rows{
@JacksonXmlProperty(localName = "entry")
public entry[] entries;
}
@JacksonXmlRootElement(localName = "entry")
@JsonIgnoreProperties(ignoreUnknown = true)
public static class entry{
@JacksonXmlProperty(isAttribute = true)
private String align;
@JacksonXmlProperty(isAttribute = true)
private String valign;
@JacksonXmlText
public String value;
}
我一直在为$ $获取Null指针异常b $ b rows [entry] - > Object [] [2])
I keep getting a Null pointer exception for
rows["entry"]->Object[][2])
推荐答案
试试这个:
@JacksonXmlRootElement(localName = "row")
public static class rows {
@JacksonXmlElementWrapper(useWrapping=false)
@JacksonXmlProperty(localName = "entry")
public entry[] entries;
}
public static class entry {
@JacksonXmlProperty(isAttribute = true)
private String align;
@JacksonXmlProperty(isAttribute = true)
private String valign;
@JacksonXmlText
public String value;
}
这篇关于如何使用Jackson将带有重复XML标记的XML解析为POJO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文