我有这个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;
...
}
如果可能的话,帮我实现。
更新 我试图做的事情:
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;
}
当去解组时,只解析带注释对象的第一个元素。另一个是null。当我先评论然后第二次解析时。
答案 0 :(得分:1)
我不认为您使用JAXB参考实现可以尝试做什么。
但是,如果您可以更改实现,EclipseLink MOXy会提供应该解决您问题的@XmlPath:
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