Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert object into JAXBElement

Tags:

java

jaxb

jaxb2

I want to implement a method which returns JAXBElement following is the code

@XmlRootElement(name = "history")
@XmlAccessorType(XmlAccessType.FIELD)
public class IBHistoryInfo {

     @XmlElement(name="trade")
     private List<IBTradeInfo> mTrade;

     public void updateTradeValue(int reqId, String date, double open, double high, double low,
                                  double close, int volume, int count, double WAP, boolean hasGaps){



        IBTradeInfo info = new IBTradeInfo();
        info.setReqId(reqId);
        info.setDate(date);
        info.setOpen(open);
        info.setHigh(high);
        info.setLow(low);
        info.setClose(close);
        info.setVolume(volume);
        info.setCount(count);
        info.setWap(WAP);
        info.setHasGaps(hasGaps);
        this.setTradeInfo(info);

     }
      public void setTradeInfo(IBTradeInfo tradeinfo){
        mTrade.add(tradeinfo);
    }

       public List<IBTradeInfo> getTradeInfo(){
         if (mTrade == null) {
                mTrade = new ArrayList<IBTradeInfo>();
            }
            return this.mTrade;


    }
}

Now i don't know how to creat a method which returns JAXBElement in the above class

for example

 public JAXBElement<IBTradeInfo> getTradeXML(){

 return mTrade

}
like image 654
Hunt Avatar asked Apr 18 '11 11:04

Hunt


1 Answers

The following is how you could implement the getTradeXML() method:

public JAXBElement<IBTradeInfo> getTradeXML(){
    if(null == mTrade || mTrade.size() == 0) {
        return null;
    }
    IBTradeInfo tradeInfo = mTrade.get(0);
    QName qname = new QName("http://www.example.com", "trade-info");
    return new JAXBElement(qname, IBTradeInfo.class, tradeInfo);
}
like image 185
bdoughan Avatar answered Sep 30 '22 09:09

bdoughan