2017-06-16 48 views
1

我有這樣的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; 
} 

當去解組解析的帶註釋的對象則只有第一個元素。另一個是空的。當我先評論然後第二個被解析。

回答

1

我不認爲你想要做什麼可以用JAXB參考實現。

但是,如果你可以改變的實現,EclipseLink的莫西報價應該可以解決你的問題@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

+0

THX您的答覆。我認爲這是我需要的。但我試圖安裝MOXY,但沒有成功。我使用的WebSphere App容器已經有JAXB Provider。我如何使用它。我嘗試將類加載器選項更改爲PARENT_LAST,但之後發生錯誤com.ibm.ws.webcontainer.exception.WebAppNotLoadedException:加載webapp失敗:javax.servlet.ServletContainerInitializer:Provider org.springframework.web.SpringServletContainerInitializer不是子類型。 WebSphere 8.5.5.11 –

+0

您是否按照以下說明操作:http://www.eclipse.org/eclipselink/documentation/2.5/solutions/websphere.htm? –

+0

我認爲這應該是另一個問題。謝謝 –