简体   繁体   English

JAXB集合映射

[英]JAXB collection mapping

I'm new in dealing with XML in Java, but one of services I use returns it as result. 我是使用Java处理XML的新手,但是我使用的一种服务将其返回作为结果。 Until now, I've dealt with mapping XML into POJO, using @XmlRootElement -like annotations. 到目前为止,我一直在使用@XmlRootElement的注释将XML映射到POJO。 But now I have absolutely no idea to do with this document: 但是现在我完全不知道该文档:

<?xml version="1.0" encoding="windows-1251"?>
<response>
    <status>
        <code>0</code>
    </status>
    <result>
        <limit>2</limit>
        ...

        <data>
            <row0>
                <ID>85427</ID>
                <name>Default</name>
                <siteID>40628</siteID>
                 ... some elements
            </row0>
        </data>
    </result>
</response>

Until now, I used these classes to bind XML (except 'data' node) into POJO: 到目前为止,我使用这些类将XML(“数据”节点除外)绑定到POJO:

@XmlRootElement(name="response")
public class Response {

    private Status status;

    private String result;

    public Status getStatus() {
        return status;
    }

    @XmlElement(name = "status")
    public void setStatus(Status status) {
        this.status = status;
    }

    public String getResult() {
        return result;
    }

    @XmlElement(name ="result")
    public void setResult(String result) {
        this.result = result;
    }
}



@XmlRootElement(name = "status")
public class Status {

    private String ID;
    private String code;
    private String error;

    public String getID() {
        return ID;
    }

    @XmlElement(name = "ID")
    public void setID(String ID) {
        this.ID = ID;
    }

    public String getCode() {
        return code;
    }

    @XmlElement(name = "code")
    public void setCode(String code) {
        this.code = code;
    }

    public String getError() {
        return error;
    }

    @XmlElement(name = "error")
    public void setError(String error) {
        this.error = error;
    }
}

But now I need to bind content as collection of elements. 但是现在我需要将内容绑定为元素集合。 I've looked for examples, and everywhere people use specific tag to define root element for collection's item, but in this document, root tags will be as <row0> , <row1> etc. 我查找了示例,到处都有人使用特定标签来定义集合项的根元素,但在本文档中,根标签将为<row0><row1>等。

I use Jackson, which, if I understand correctly, uses JAXB annotations to define XML to POJO bind rules. 我使用Jackson,如果我理解正确的话,可以使用JAXB批注定义XML到POJO绑定规则。 So could this deal be solved this way, or I have to manipulate this document in DOM-style? 那么是否可以通过这种方式解决此问题,或者我必须以DOM风格操作此文档?

You can solve your problem by using something like this : 您可以使用以下方法解决问题:

Create your Row class that represents your <row0>,<row1> etc... and map it with JAXB like you would do it normally. 创建代表<row0>,<row1>等的Row类,并像通常那样使用JAXB映射它。

Then create a class that extends XmlAdapter<List<Row>,List<Element>> and define the abstracts methods marshall and unmarshall. 然后创建一个扩展XmlAdapter<List<Row>,List<Element>>并定义摘要方法marshall和unmarshall。

Here is some Javadoc to help you : 这是一些Javadoc可以帮助您:

XmlAdapter : http://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/adapters/XmlAdapter.html XmlAdapter: http ://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/adapters/XmlAdapter.html

Element : http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Element.html 元素: http : //docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Element.html

Then create a Data class : 然后创建一个Data类:

public class Data{

    private List<Row> rows;

    public List<Row> getRows() {
        return rows;
    }

    @XmlAnyElement
    @XmlJavaTypeAdapter(MyRowsAdapter.class)
    public void setRows(List<Row> result) {
        this.rows = rows;
    }

}

Then you can add this mapping to your Response class : 然后,您可以将此映射添加到您的Response类:

private Data data;

public Data getData() {
    return data;
}

@XmlElement(name="data")
public void setData(Data data) {
    this.data = data;
}

Note that for this solution to work, your <data> element must only contains elements like your Row. 请注意,为了使此解决方案起作用,您的<data>元素必须仅包含像Row这样的元素。

Also, you cannot use @XmlElementWrapper instead of using a Data class because of a bug in JAXB which make incompatible @XmlElementWrapper and @XmlJavaTypeAdapter : https://java.net/jira/browse/JAXB-787 另外,由于JAXB中的错误导致@XmlElementWrapper和@XmlJavaTypeAdapter不兼容,因此您不能使用@XmlElementWrapper而不是使用Data类: https ://java.net/jira/browse/JAXB-787

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

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