convert object into JAXBElement
Asked Answered
R

2

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

}
Rosaceous answered 18/4, 2011 at 11:16 Comment(1)
You have class but you don't have a question...Randall
A
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);
}
Americanism answered 18/4, 2011 at 13:13 Comment(7)
Can i get all the elements in the list , you can say the whole JAXBElement<IBHistoryInfo> ??Rosaceous
@Rosaceous - 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?Americanism
@Rosaceous - It appears your last comment was cut off.Americanism
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 JAXBElementRosaceous
@Rosaceous - 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.Americanism
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 hereRosaceous
@Rosaceous - 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.Americanism
G
-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.

Generation answered 18/4, 2011 at 13:14 Comment(1)
That constructor does not exist for JAXBElement. You need to also supply a QName to represent what the root element will be.Americanism

© 2022 - 2024 — McMap. All rights reserved.