简体   繁体   中英

convert object into JAXBElement

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

}

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);
}

I believe, you can only return 1 element at a time. In this case, you possibly need to write something like:

public JAXBElement<IBTradeInfo> getTradeXML(){
  return new JAXBElement<IBTradeInfo>(mTrade.get(0), IBTradeInfo.class);
}

Just a guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM