2013-08-01 57 views
1

我正在研究用於查詢Eve-Central的工具。夏娃中心查詢XML格式返回,因爲這樣的:使用JAXB將XML解析爲不同的數據結構

<evec_api version="2.0" method="marketstat_xml"> 
    <marketstat> 
     <type id="608"> 
      <buy> 
       <volume>74018</volume> 
       <avg>68274.08</avg> 
       <max>410000.01</max> 
       <min>25000.23</min> 
       <stddev>97055.39</stddev> 
       <median>50000.00</median> 
       <percentile>255537.71</percentile> 
      </buy> 
      <sell> 
       <volume>15324</volume> 
       <avg>477255.37</avg> 
       <max>1914490.39</max> 
       <min>175000.00</min> 
       <stddev>266422.73</stddev> 
       <median>407994.99</median> 
       <percentile>309282.09</percentile> 
      </sell> 
      <all> 
       <volume>87592</volume> 
       <avg>107228.56</avg> 
       <max>486000.00</max> 
       <min>10.00</min> 
       <stddev>123725.64</stddev> 
       <median>50000.00</median> 
       <percentile>47814.35</percentile> 
      </all> 
     </type> 
    </marketstat> 
</evec_api> 

我想使用註釋這種格式解析爲以下數據類:

public class MarketStatObject { 

    private int id; 
    private MarketStatObjectStats buy; 
    private MarketStatObjectStats sell; 
    private MarketStatObjectStats all; 

    .... 
} 

public class MarketStatObjectStats { 

    // values 
    private long volume; 
    private double average; 
    private double minimum; 
    private double maximum; 
    private double stddev; 
    private double median; 
    private double percentile; 

    .... 
} 

現在,註釋爲MarketStatObjectStats對象應該是相當自我解釋,但我趕上「類型」的XML結構是買/賣/所有統計的包裝。

什麼是解決這個問題的最好方法?我不想類型ID是一個清單,因爲這是完全沒有必要的......

回答

2

注:我是EclipseLink JAXB (MOXy)鉛和JAXB (JSR-222)專家小組的成員。

你可以使用莫西的@XmlPath擴展該用例:

MarketStatObject

package forum17988539; 

import javax.xml.bind.annotation.*; 
import org.eclipse.persistence.oxm.annotations.XmlPath; 

@XmlRootElement(name="evec_api") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class MarketStatObject { 

    @XmlPath("marketstat/type/@id") 
    private int id; 

    @XmlPath("marketstat/type/buy") 
    private MarketStatObjectStats buy; 

    @XmlPath("marketstat/type/sell") 
    private MarketStatObjectStats sell; 

    @XmlPath("marketstat/type/all") 
    private MarketStatObjectStats all; 

} 

演示

標準JAXB運行時API用於轉換您的XML /從對象。

package forum17988539; 

import java.io.File; 
import javax.xml.bind.*; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(MarketStatObject.class); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     File xml = new File("src/forum17988539/input.xml"); 
     MarketStatObject marketStat = (MarketStatObject) unmarshaller.unmarshal(xml); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(marketStat, System.out); 
    } 

} 

更多信息

1
@XmlRootElement(name = "evec_api") 
public class EvecApi { 
    @XmlElement MarketStat marketstat; 
} 

public class MarketStat { 
    @XmlElement MarketStatObject type; 
} 

public class MarketStatObject { 
    @XmlAttribute Integer id; 
    @XmlElement MarketStatObjectStats buy; 
    @XmlElement MarketStatObjectStats sell; 
    @XmlElement MarketStatObjectStats all; 
} 

public class MarketStatObjectStats { 
    @XmlElement long volume; 
    @XmlElement double average; 
    @XmlElement double minimum; 
    @XmlElement double maximum; 
    @XmlElement double stddev; 
    @XmlElement double median; 
    @XmlElement double percentile; 
}