8

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

}
Hunt
  • 8,215
  • 28
  • 116
  • 256

2 Answers2

6

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);
}
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Can i get all the elements in the list , you can say the whole JAXBElement ?? – Hunt Apr 18 '11 at 14:04
  • @Hunt - You could create a JAXBElement on any object in the same way. JAXBElement is just a wrapper object to provide info about a wrapper element when necessary. Is this the use case you are trying to support? – bdoughan Apr 18 '11 at 14:14
  • @Hunt - It appears your last comment was cut off. – bdoughan Apr 18 '11 at 15:28
  • Well i was saying that i am implementing this and it is not a use case. so is there a way to get all the elements in JAXBElement – Hunt Apr 18 '11 at 16:47
  • @Hunt - If you want all of the instances of IBTradeInfo in a single instance of JAXBElement, then you will need to create a wrapper object (with a collection of IBTradeInfo) and build a JAXBElement on that. You could also create the JAXBElement on the instance of IBHistoryInfo. – bdoughan Apr 18 '11 at 17:20
  • Wrapper of IBTradeInfo but i have put it in a list already which accumulate all the records , can you please tell me what exactly you mean by wrapper object here – Hunt Apr 18 '11 at 17:47
  • @Hunt - By "wrapper object" I mean you need JAXBElement to hold a "wrapper object" that has a List property that contains instances of IBTradeInfo. Your class IBHistoryInfo could serve this role. – bdoughan Apr 18 '11 at 17:54
-1

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.

weekens
  • 8,064
  • 6
  • 45
  • 62
  • That constructor does not exist for JAXBElement. You need to also supply a QName to represent what the root element will be. – bdoughan Apr 18 '11 at 13:22