解组类型列表 [英] Unmarshal List to Types

查看:67
本文介绍了解组类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个xml

<data-set>
    <list-property name="columns">...</list-property>
    <list-property name="resultSet">...</list-property>
<data-set>

需要将其解组为对象:

public class DataSet {

    private Columns columns;

    private ResultSet resultSet;
    ...
}

如果可能的话,请帮助我实现这一目标.

Help me to implement that if it possible.

更新我试图做的事:

public class DataSet {

    @XmlElement("list-property")
    @XmlJavaTypeAdapter(DataSetListPropertyAdapter.class)
    private Columns columns;

    @XmlElement("list-property")
    @XmlJavaTypeAdapter(DataSetListPropertyAdapter.class)
    private ResultSet resultSet;

    ...
}

public class DataSetListPropertyAdapter extends XmlAdapter<ListProperty, ListProperty> {
@Override
public ListProperty unmarshal(ListProperty v) throws Exception {
    ListProperty listProperty;
    switch (v.getName()) {
        case "columns":
            listProperty = new Columns();
            break;
        case "resultSet":
            listProperty = new ResultSet();
            break;
        default:
            listProperty = new ListProperty();
    }
    listProperty.setStructure(v.getStructure());
    return listProperty;
}

    @Override
    public ListProperty marshal(ListProperty v) throws Exception {
        return v;
    }
}

public class Columns extends ListProperty {
public Columns() {
    name = "columns";
}
}

public class ListProperty extends NamedElement implements PropertyType{

@XmlElement(name = "structure")
private List<Structure> structure = new ArrayList<>();
}

@XmlTransient
public class NamedElement {

@XmlAttribute(name = "name", required = true)
protected String name;
}

解组时,仅解析带注释的对象的第一个元素.另一个为空.当我先发表评论时,第二则被解析.

When go unmarshalling then only first element of annotated objects parsed. Another is null. When I comment first then second becames parsed.

推荐答案

我不认为您尝试使用JAXB参考实现来实现.

I do not think what you're trying to do is possible with JAXB reference implementation.

但是,如果您可以更改实现,则EclipseLink MOXy提供@XmlPath应该可以解决您的问题:

However, if you can change implementation, EclipseLink MOXy offer the @XmlPath that should resolve your problem :

public class DataSet {

    @XmlPath("node[@name='columns']")
    @XmlJavaTypeAdapter(DataSetListPropertyAdapter.class)
    private Columns columns;

    @XmlPath("node[@name='resultSet']")
    @XmlJavaTypeAdapter(DataSetListPropertyAdapter.class)
    private ResultSet resultSet;

    ...
}

有关@XmlPath的更多信息: http://www.eclipse.org/eclipselink/documentation/2.4/moxy/advanced_concepts005.htm

More on @XmlPath : http://www.eclipse.org/eclipselink/documentation/2.4/moxy/advanced_concepts005.htm

这篇关于解组类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆