解组类型列表

编程入门 行业动态 更新时间:2024-10-24 17:19:24
本文介绍了解组类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个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的更多信息: www.eclipse/eclipselink/documentation/2.4/moxy/advanced_concepts005.htm

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

更多推荐

解组类型列表

本文发布于:2023-11-22 15:13:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!