如何使用 Jackson 将带有重复 XML 标签的 XML 解析为 POJO?

How to parse XML with repeated XML tags into a POJO using Jackson?

我在使用 Jackson 将此 XML 解析为 POJO 时遇到问题。我已经阅读了之前关于使 类 将 XML 反序列化为 POJOS 的所有描述,但我一直收到 Null 指针或 not END of element 警告。我非常困惑,非常感谢任何帮助。

输入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;

}

我不断收到
的空指针异常 行["entry"]->对象[][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;
}