简体   繁体   English

使用Jackson XML jar将XML数据转换为POJO?

[英]Convert XML data to POJO with Jackson XML jar?

Am work on java project with XML data from another web-service. 我正在使用来自另一个Web服务的XML数据处理Java项目。 Am new to Jackson framework. 是Jackson框架的新手。 Create the POJO file but while mapping it show error like Unrecognized field column. 创建POJO文件,但在映射时显示错误,如无法识别的字段列。 Don't know how to map my response 不知道如何绘制我的回应

XML Response XML回应

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CellSet>
    <Row key="MNg==">
        <Cell column="ZG9ybWFudF9kZXRhaW29u" timestamp="1563189660088">MjAQ==</Cell>
        <Cell column="ZG9yxzOkV5ZXBsdXM=" timestamp="1563189660088">RI0</Cell>
        <Cell column="ZG9ybWFudF9kZWxzOl=" timestamp="1563189660088"/>
        <Cell column="ZG9ybaWxzOlVDSUM=" timestamp="1563189660088">Mg==</Cell>
        <Cell column="ZG9ybWFudF9kZXRhYXRl" timestamp="1563189660088">MMQ==</Cell>
        <Cell column="ZG9ybWxzOlpveWE=" timestamp="1563189660088"/>
        <Cell column="ZzOndhdGNoZXM=" timestamp="1563189660088"/>
    </Row>
</CellSet>

CellSet Class CellSet类别

@JacksonXmlRootElement(localName = "CellSet")
public final class CellSet {

public CellSet() {}

@JacksonXmlElementWrapper(localName = "Row")
private Row[] rows;

public Row[] getRows() {
    return rows;
}

}

Row Class 行类

public final class Row {

public Row() {}

@JacksonXmlElementWrapper(localName = "Cell")
private Cell[] cells;

public Cell[] getCells() {
    return cells;
}

}

Cell Class 单元类别

public final class Cell {

public Cell() {
    // TODO Auto-generated constructor stub
}

@JacksonXmlProperty(localName = "column", isAttribute = true)
private String column;

@JacksonXmlProperty(localName = "timestamp", isAttribute = true)
private String timestamp;

public String getColumn() {
    return column;
}

public String getTimestamp() {
    return timestamp;
}
}

Help me to solve this issue. 帮我解决这个问题。

First of all, looking at the xml document and the Java bean tree, I made the assumption that a response can have multiple Row elements under the CellSet root element. 首先,查看xml文档和Java bean树,我假设一个响应在CellSet根元素下可以有多个Row元素。

So, given the above assumption, there are several problems with your object model. 因此,基于上述假设,对象模型存在几个问题。 The first is the misuse of the @JacksonXmlElementWrapper . 第一个是@JacksonXmlElementWrapper的滥用。 This annotation signifies that an array of XML elements is wrapped by a higer-hierarchy element. 此注释表示XML元素数组由higer-hierarchy元素包装。 so when you write 所以当你写

@JacksonXmlElementWrapper(localName = "Row")
private Row[] rows;

jackson expects an input like this 杰克逊期望这样的输入

<Row>  // xml wrapper
  <rows>  // xml array 
  <rows>
  <rows>
</Row>

in fact, you need to tell jackson that the row array is unwrapped like this 实际上,您需要告诉杰克逊这样的行数组是未包装的

@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "Row")
private Row[] rows;

and same for cell array. 和单元格数组相同。

the other problem is that Row class does not have a target for key attribute and Cell does not have a target for the text value of the cell element. 另一个问题是Row类没有键属性的目标,而Cell没有单元格元素的文本值的目标。
the complete solution is given below 完整的解决方案如下

@JacksonXmlRootElement(localName = "CellSet")
public class CellSet {

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "Row")
    public Row[] rows;
}

public class Row {

    @JacksonXmlProperty(localName = "key", isAttribute = true)
    public String key;

    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "Cell")
    public Cell[] cells;
}

public class Cell {

    @JacksonXmlText()
    public String value;

    @JacksonXmlProperty(localName = "column", isAttribute = true)
    public String column;

    @JacksonXmlProperty(localName = "timestamp", isAttribute = true)
    public String timestamp;

}

last note: your classes are also missing setter methods (this is deserialization/unmarshalling, right?) in the above solution, all variables were made public for brevity 最后一点:在上述解决方案中,您的类也缺少setter方法(这是反序列化/解组,对吗?),所有变量都为了简洁起见公开

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM