jaxb XmlAdapter unmarshall an object from the same xml - jaxb

take a look at this code :
#XmlRootElement
class Course {
private int id;
private String name;
public Course() {}
public Course(int id, String name) {
super();
this.id = id;
this.name = name;
}
#XmlAttribute(name = "id")
public int getId() {
return id;
}
#XmlAttribute(name = "name")
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
class CourseAdapter extends XmlAdapter<Integer, Course>{
#Override
public Course unmarshal(Integer v) throws Exception {
// what to do hereeeeeeeeeeeeeeee????!!!!
// I want the Course with the id = v unmarshalled from
// the same xml I am unmarshalling at the moment
}
#Override
public Integer marshal(Course v) throws Exception {
return v.getId();
}
}
#XmlRootElement
class Offering {
private int id;
private Course course;
private int capacity;
public Offering() {}
public Offering(int id, Course course, int capacity) {
super();
this.id = id;
this.course = course;
this.capacity = capacity;
}
#XmlAttribute(name = "id")
public int getId() {
return id;
}
#XmlJavaTypeAdapter(CourseAdapter.class)
#XmlAttribute(name = "course")
public Course getCourse() {
return course;
}
#XmlAttribute(name = "capacity")
public int getCapacity() {
return capacity;
}
public void setId(int id) {
this.id = id;
}
public void setCourse(Course course) {
this.course = course;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
}
#XmlRootElement
class Department {
private String name;
private ArrayList<Course> courses;
private ArrayList<Offering> offerings;
public Department(){}
public Department(String name, ArrayList<Course> courses, ArrayList<Offering> offerings) {
super();
this.name = name;
this.courses = courses;
this.offerings = offerings;
}
#XmlAttribute(name = "name")
public String getName() {
return name;
}
#XmlElement
public ArrayList<Course> getCourses() {
return courses;
}
#XmlElement
public ArrayList<Offering> getOfferings() {
return offerings;
}
public void setName(String name) {
this.name = name;
}
public void setCourses(ArrayList<Course> courses) {
this.courses = courses;
}
public void setOfferings(ArrayList<Offering> offerings) {
this.offerings = offerings;
}
}
I have marshalled my Department and this is the result:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<department name="ece">
<courses id="1" name="farsi"/>
<courses id="2" name="dini"/>
<courses id="2" name="riazi"/>
<offerings capacity="10" course="1" id="1"/>
<offerings capacity="20" course="2" id="2"/>
</department>
the problem is I do not know how to unmarshal course with the id = v from this very xml which is being unmarshalled via "unmarshal" function of CourseAdapter .

You can use #XmlID/#XmlIDREF for this use case without requiring an XmlAdapter.
http://blog.bdoughan.com/2010/10/jaxb-and-shared-references-xmlid-and.html

Related

issues with parcelable extension: getting unmarshalling unknown type code exception

long story short, I have one settlementItemeBase class as my father and two children. I want to make the father class parcelable so as extension happens, my two child classes be parcelable as well. I don't exactly know what I'm doing right or wrong. I searched little bit but nothing helped me.
here are my classes:
SettlementItemBase:
public class SettlementItemBase implements Parcelable{
public SettlementItemBase(){}
protected SettlementItemBase(Parcel in) {
}
public static final Creator<SettlementItemBase> CREATOR = new Creator<SettlementItemBase>() {
#Override
public SettlementItemBase createFromParcel(Parcel in) {
return new SettlementItemBase(in);
}
#Override
public SettlementItemBase[] newArray(int size) {
return new SettlementItemBase[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
}
}
first child class:
public class FirstClass extends SettlementItemBase {
private int id;
private int cardId;
private String cardNumber;
private String expDate;
private String currency;
private String url;
public FirstClass(){
id = 0;
cardId = 0;
cardNumber = "";
expDate = "";
currency = "";
url = "";
}
protected FirstClass(Parcel in) {
super(in);
id = in.readInt();
cardId = in.readInt();
cardNumber = in.readString();
expDate = in.readString();
currency = in.readString();
url = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeInt(cardId);
dest.writeString(cardNumber);
dest.writeString(expDate);
dest.writeString(currency);
dest.writeString(url);
}
public int getId() {
return id;
}
public int getCardId() {
return cardId;
}
public String getCardNumber() {
return cardNumber;
}
public String getExpDate() {
return expDate;
}
public String getCurrency() {
return currency;
}
public String getUrl() {
return url;
}
}
second child class:
public class SecondClass extends SettlementItemBase{
private int id;
private String currency;
private String accountNumber;
private String ibanNumber;
public SecondClass(int id, String currency,
String accountNumber, String ibanNumber){
this.id = id;
this.currency = currency;
this.accountNumber = accountNumber;
this.ibanNumber = ibanNumber;
}
protected SecondClass(Parcel in){
super(in);
id = in.readInt();
currency = in.readString();
accountNumber = in.readString();
ibanNumber = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(currency);
dest.writeString(accountNumber);
dest.writeString(ibanNumber);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCurrency() {
return currency;
}
public String getAccountNumber() {
return accountNumber;
}
public String getIbanNumber() {
return ibanNumber;
}
}
I'm passing and getting an ArrayList of both child class' items with intent putParcelableArrayListExtra and getParcelableArrayListExtra methods and get the following error:
Parcel android.os.Parcel#2d0fde33: Unmarshalling unknown type code 3276849 at offset 172
any help would be appreciated 3>
well I'm posting this for those who may run into the same problem as me.
the problem was solved after adding the Creator statement to the child classes.
for one of the child classes it would be like:
public static final Creator<FirstClass> CREATOR = new Creator<FirstClass>() {
#Override
public FirstClass createFromParcel(Parcel in) {
return new FirstClass(in);
}
#Override
public FirstClass[] newArray(int size) {
return new FirstClass[size];
}
};
hope this helps. 3>

Field attribute is coming as null while unmarshalling an xml file

Hi I am trying to convert an xml file into Java Objects using JAXB and I am very new to java. I have created the pojo classes and added some annotations but I am not sure whether they are right? I have spent hours in google but couldn't find what is wrong.
This is my xml :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<question id="1">
<answers>
<answername>java is a programming language</answername>
<id>101</id>
<postedby>ravi</postedby>
</answers>
<answers>
<answername>java is a platform</answername>
<id>102</id>
<postedby>john</postedby>
</answers>
<questionname>What is java?</questionname>
<marks set=50>
<longAnswer set=45/>
<shortAnswer set=30/>
</marks>
</question>
Pojo classes:
#XmlRootElement(name="question")
public class Question {
private int id;
private String questionname;
private List<Answer> answers;
private List<Marks> marks;
public Question() {}
public Question(int id, String questionname, List<Answer> answers, List<Marks> marks) {
super();
this.id = id;
this.questionname = questionname;
this.answers = answers;
this.marks = marks;
}
#XmlElement(name="marks")
public List<Marks> getMarks() {
return marks;
}
public void setMarks(List<Marks> marks) {
this.marks = marks;
}
#XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#XmlElement
public String getQuestionname() {
return questionname;
}
public void setQuestionname(String questionname) {
this.questionname = questionname;
}
#XmlElement
public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
}
public class Answer {
private int id;
private String answername;
private String postedby;
public Answer() {}
public Answer(int id, String answername, String postedby) {
super();
this.id = id;
this.answername = answername;
this.postedby = postedby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAnswername() {
return answername;
}
public void setAnswername(String answername) {
this.answername = answername;
}
public String getPostedby() {
return postedby;
}
public void setPostedby(String postedby) {
this.postedby = postedby;
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
public class Marks {
private LongAnswer longAnswer ;
private ShortAnswer shortAnswer;
private String set;
#XmlAttribute
public String getSet() {
return set;
}
public void setSet(String set) {
this.set = set;
}
#XmlElement(name="longAnswer")
public LongAnswer getLongAnswer() {
return longAnswer;
}
public void setLongAnswer(LongAnswer longAnswer) {
this.longAnswer = longAnswer;
}
#XmlElement(name="shortAnswer")
public ShortAnswer getShortAnswer() {
return shortAnswer;
}
public void setShortAnswer(ShortAnswer shortAnswer) {
this.shortAnswer = shortAnswer;
}
}
public class LongAnswer {
private String set;
public String getSet() {
return set;
}
public void setSet(String set) {
this.set = set;
}
public class ShortAnswer {
private String set;
public String getSet() {
return set;
}
public void setSet(String set) {
this.set = set;
}
}
Can anyone tell me how to annotate the 'marks' model class and how to set 'longAnswer' and 'shortAnswer' field. Because i am getting null values for them.
You should annotate your set properties with #XmlAttribute. Otherwise it looks quite fine.
What you could also do is create an XML Schema for you XML and compile it.

Unmarshalling jaxB doesn't work, objects null

I have a problem with jaxB. There is no exception and here is my xml ;
<event>
<event_datetimeGMT>2015-02-27 17:00</event_datetimeGMT>
<gamenumber>443364144</gamenumber>
<sporttype>Basketball</sporttype>
<league>Adriatic</league>
<IsLive>No</IsLive>
<participants>
<participant>
<participant_name>Cedevita</participant_name>
<contestantnum>1001</contestantnum>
<rotnum>1001</rotnum>
<visiting_home_draw>Home</visiting_home_draw>
</participant>
<participant>
<participant_name>Mega Leks</participant_name>
<contestantnum>1002</contestantnum>
<rotnum>1002</rotnum>
<visiting_home_draw>Visiting</visiting_home_draw>
</participant>
</participants>
<periods>
<period>
<period_number>0</period_number>
<period_description>Game</period_description>
<periodcutoff_datetimeGMT>2015-02-27 17:00</periodcutoff_datetimeGMT>
<period_status>I</period_status>
<period_update>open</period_update>
<spread_maximum>500</spread_maximum>
<moneyline_maximum>500</moneyline_maximum>
<total_maximum>500</total_maximum>
<moneyline>
<moneyline_visiting>583</moneyline_visiting>
<moneyline_home>-720</moneyline_home>
</moneyline>
<spread>
<spread_visiting>12</spread_visiting>
<spread_adjust_visiting>-104</spread_adjust_visiting>
<spread_home>-12</spread_home>
<spread_adjust_home>-106</spread_adjust_home>
</spread>
<total>
<total_points>164</total_points>
<over_adjust>-101</over_adjust>
<under_adjust>-109</under_adjust>
</total>
</period>
</periods>
here is main class ;
#XmlRootElement (name = "pinnacle_line_feed")
public class PinnacleLineFeed {
private Long PinnacleFeedTime;
private Long lastContest;
private Long lastGame;
private List<Event> events;
#XmlAttribute(name = "PinnacleFeedTime")
public Long getPinnacleFeedTime() {
return PinnacleFeedTime;
}
public void setPinnacleFeedTime(Long pinnacleFeedTime) {
PinnacleFeedTime = pinnacleFeedTime;
}
#XmlAttribute (name = "lastContest")
public Long getLastContest() {
return lastContest;
}
public void setLastContest(Long lastContest) {
this.lastContest = lastContest;
}
#XmlAttribute (name = "lastGame")
public Long getLastGame() {
return lastGame;
}
public void setLastGame(Long lastGame) {
this.lastGame = lastGame;
}
#XmlElement (name = "events")
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
}
you can see full xml from the url which i shared at my first post
and here is my pojos ;
public class Event {
private String event_datetimeGMT;
private Long gamenumber;
private String sporttype;
private String league;
private String IsLive;
private List<Participant> participants;
private List<Period> periods;
#XmlAttribute (name = "event_datetimeGMT")
public String getEvent_datetimeGMT() {
return event_datetimeGMT;
}
public void setEvent_datetimeGMT(String event_datetimeGMT) {
this.event_datetimeGMT = event_datetimeGMT;
}
#XmlAttribute (name = "gamenumber")
public Long getGamenumber() {
return gamenumber;
}
public void setGamenumber(Long gamenumber) {
this.gamenumber = gamenumber;
}
#XmlAttribute (name = "sporttype")
public String getSporttype() {
return sporttype;
}
public void setSporttype(String sporttype) {
this.sporttype = sporttype;
}
#XmlAttribute (name = "league")
public String getLeague() {
return league;
}
public void setLeague(String league) {
this.league = league;
}
#XmlAttribute (name = "IsLive")
public String getIsLive() {
return IsLive;
}
public void setIsLive(String isLive) {
this.IsLive = isLive;
}
#XmlElement(name = "participant")
public List<Participant> getParticipants() {
return participants;
}
public void setParticipants(List<Participant> participants) {
this.participants = participants;
}
#XmlElement(name = "period")
public List<Period> getPeriods() {
return periods;
}
public void setPeriods(List<Period> periods) {
this.periods = periods;
}
}
public class Participants {
private List<Participant> participants;
#XmlElement(name = "participant")
public List<Participant> getParticipants() {
return participants;
}
public void setParticipants(List<Participant> participants) {
this.participants = participants;
}
}
public class Participant {
private String participant_name;
private Long contestantnum;
private Long rotnum;
private String visiting_home_draw;
#XmlAttribute(name = "participant_name")
public String getParticipant_name() {
return participant_name;
}
public void setParticipant_name(String participant_name) {
this.participant_name = participant_name;
}
#XmlAttribute (name = "contestantnum")
public Long getContestantnum() {
return contestantnum;
}
public void setContestantnum(Long contestantnum) {
this.contestantnum = contestantnum;
}
#XmlAttribute (name = "rotnum")
public Long getRotnum() {
return rotnum;
}
public void setRotnum(Long rotnum) {
this.rotnum = rotnum;
}
#XmlAttribute (name = "visiting_home_draw")
public String getVisiting_home_draw() {
return visiting_home_draw;
}
public void setVisiting_home_draw(String visiting_home_draw) {
this.visiting_home_draw = visiting_home_draw;
}
}
public class Periods {
private List<Period> periods;
#XmlElement(name = "period")
public List<Period> getPeriods() {
return periods;
}
public void setPeriods(List<Period> periods) {
this.periods = periods;
}
}
public class Period {
private int period_number;
private String period_description;
private String periodcutoff_datetimeGMT;
private String period_status;
private String period_update;
private int spread_maximum;
private int moneyline_maximum;
private int total_maximum;
private Moneyline moneyline;
private Spread spread;
private Total total;
#XmlAttribute(name = "period_number")
public int getPeriod_number() {
return period_number;
}
public void setPeriod_number(int period_number) {
this.period_number = period_number;
}
#XmlAttribute(name = "period_description")
public String getPeriod_description() {
return period_description;
}
public void setPeriod_description(String period_description) {
this.period_description = period_description;
}
#XmlAttribute(name = "periodcutoff_datetimeGMT")
public String getPeriodcutoff_datetimeGMT() {
return periodcutoff_datetimeGMT;
}
public void setPeriodcutoff_datetimeGMT(String periodcutoff_datetimeGMT) {
this.periodcutoff_datetimeGMT = periodcutoff_datetimeGMT;
}
#XmlAttribute(name = "period_status")
public String getPeriod_status() {
return period_status;
}
public void setPeriod_status(String period_status) {
this.period_status = period_status;
}
#XmlAttribute(name = "period_update")
public String getPeriod_update() {
return period_update;
}
public void setPeriod_update(String period_update) {
this.period_update = period_update;
}
#XmlAttribute(name = "spread_maximum")
public int getSpread_maximum() {
return spread_maximum;
}
public void setSpread_maximum(int spread_maximum) {
this.spread_maximum = spread_maximum;
}
#XmlAttribute(name = "moneyline_maximum")
public int getMoneyline_maximum() {
return moneyline_maximum;
}
public void setMoneyline_maximum(int moneyline_maximum) {
this.moneyline_maximum = moneyline_maximum;
}
#XmlAttribute(name = "total_maximum")
public int getTotal_maximum() {
return total_maximum;
}
public void setTotal_maximum(int total_maximum) {
this.total_maximum = total_maximum;
}
#XmlElement (name = "moneyline")
public Moneyline getMoneyline() {
return moneyline;
}
public void setMoneyline(Moneyline moneyline) {
this.moneyline = moneyline;
}
#XmlElement (name = "spread")
public Spread getSpread() {
return spread;
}
public void setSpread(Spread spread) {
this.spread = spread;
}
#XmlElement (name = "total")
public Total getTotal() {
return total;
}
public void setTotal(Total total) {
this.total = total;
}
}
public class Moneyline {
private int moneyline_visiting;
private int moneyline_home;
#XmlAttribute(name = "moneyline_visiting")
public int getMoneyline_visiting() {
return moneyline_visiting;
}
public void setMoneyline_visiting(int moneyline_visiting) {
this.moneyline_visiting = moneyline_visiting;
}
#XmlAttribute(name = "moneyline_home")
public int getMoneyline_home() {
return moneyline_home;
}
public void setMoneyline_home(int moneyline_home) {
this.moneyline_home = moneyline_home;
}
}
public class Spread {
private int spread_visiting;
private int spread_adjust_visiting;
private int spread_home;
private int spread_adjust_home;
#XmlAttribute(name = "spread_visiting")
public int getSpread_visiting() {
return spread_visiting;
}
public void setSpread_visiting(int spread_visiting) {
this.spread_visiting = spread_visiting;
}
#XmlAttribute(name = "spread_adjust_visiting")
public int getSpread_adjust_visiting() {
return spread_adjust_visiting;
}
public void setSpread_adjust_visiting(int spread_adjust_visiting) {
this.spread_adjust_visiting = spread_adjust_visiting;
}
#XmlAttribute(name = "spread_home")
public int getSpread_home() {
return spread_home;
}
public void setSpread_home(int spread_home) {
this.spread_home = spread_home;
}
#XmlAttribute(name = "spread_adjust_home")
public int getSpread_adjust_home() {
return spread_adjust_home;
}
public void setSpread_adjust_home(int spread_adjust_home) {
this.spread_adjust_home = spread_adjust_home;
}
}
public class Total {
private int total_points;
private int over_adjust;
private int under_adjust;
#XmlAttribute(name = "total_points")
public int getTotal_points() {
return total_points;
}
public void setTotal_points(int total_points) {
this.total_points = total_points;
}
#XmlAttribute(name = "over_adjust")
public int getOver_adjust() {
return over_adjust;
}
public void setOver_adjust(int over_adjust) {
this.over_adjust = over_adjust;
}
#XmlAttribute(name = "under_adjust")
public int getUnder_adjust() {
return under_adjust;
}
public void setUnder_adjust(int under_adjust) {
this.under_adjust = under_adjust;
}
}
and my test method ;
private static void unmarshall() {
try {
JAXBContext jc = JAXBContext.newInstance(PinnacleLineFeed.class);
URL url = new URL("http://xml.pinnaclesports.com/pinnacleFeed.aspx?");
Unmarshaller unmarshaller = jc.createUnmarshaller();
PinnacleLineFeed pinnacleLineFeed = (PinnacleLineFeed) unmarshaller.unmarshal(url);
System.out.println("bakıcez");
} catch (Exception ex) {
ex.printStackTrace();
}
}
When i try to unmarshall this xml, all fields are null. And there is no exception. Can anyone help ?

Jaxb #Attribute error read attribute

I have the code and xml and xml and cannot read the xml and believe that it is for the attribute. how can I read the attribute?
This is the code:
#XmlRootElement(name = "reimpresiones")
public class RePrint {
private Integer id;
private String document;
private String numberDocument;
private String extraction;
private String client;
private String groupExtraction;
private String route;
private String deliveryNote;
private String date;
private List<RePrint> reprintList;
public RePrint() {
}
#XmlAttribute(name = "id")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#XmlElement(name = "client")
public String getClient() {
return client;
}
public void setClient(String client) {
this.client = client;
}
#XmlElement(name = "document")
public String getDocument() {
return document;
}
public void setDocument(String document) {
this.document = document;
}
#XmlElement(name = "numberDocument")
public String getNumberDocument() {
return numberDocument;
}
public void setNumberDocument(String numberDocument) {
this.numberDocument = numberDocument;
}
#XmlElement(name = "extraction")
public String getExtraction() {
return extraction;
}
public void setExtraction(String extraction) {
this.extraction = extraction;
}
#XmlElement(name = "groupExtraction")
public String getGroupExtraction() {
return groupExtraction;
}
public void setGroupExtraction(String groupExtraction) {
this.groupExtraction = groupExtraction;
}
#XmlElement(name = "route")
public String getRoute() {
return route;
}
public void setRoute(String route) {
this.route = route;
}
#XmlElement(name = "deliveryNote")
public String getDeliveryNote() {
return deliveryNote;
}
public void setDeliveryNote(String deliveryNote) {
this.deliveryNote = deliveryNote;
}
#XmlElement(name = "date")
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
#XmlElement(name = "reimprension")
public List<RePrint> getReprintList() {
return reprintList;
}
public void setReprintList(List<RePrint> reprintList) {
this.reprintList = reprintList;
}
}
This is the xml where there is a list of elements. I want to read "id" but i cannot.
<reimpresiones>
<reimpresion id="10574691840591525620140557591784">
<document>MATRICULA</document>
<numdocument><![CDATA[M142849865 ]]></numdocument>
<groupExtraction>1849986</groupExtraction>
<extraction>919767</extraction>
<client>780</client>
<deliveryNote><![CDATA[3600445640/01 ]]></deliveryNote>
<route>BUY</route>
<date>2014-05-05-17.59.57.919200</date>
</reimpresion>
<reimpresion id="14081953280539172820140537251728">
<document>MATRICULA</document>
<numdocument><![CDATA[M142849864 ]]></numdocument>
<groupExtraction>1849986</groupExtraction>
<extraction>919767</extraction>
<client>780</client>
<deliveryNote><![CDATA[3600445640/01 ]]></deliveryNote>
<route>BUY</route>
<date>2014-05-05-17.25.37.427752</date>
</reimpresion>
</reimpresiones>
Thank you

Serialize HashMap like a Collection Property

Here I got two classes "Student" and "Course":
#XmlRootElement
class Student {
private String name;
private HashMap<String, Course> courses;
public Student() {}
public Student(String name, HashMap<String, Course> courses) {
super();
this.name = name;
this.courses = courses;
}
// -------------------------getters
#XmlAttribute(name="name")
public String getName() {
return name;
}
#XmlElement(name = "course")
public HashMap<String, Course> getCourses() {
return courses;
}
//---------------------------setters
public void setName(String name) {
this.name = name;
}
public void setCourses(HashMap<String, Course> courses) {
this.courses = courses;
}
}
#XmlRootElement
class Course {
private String id;
private String name;
public Course() {}
public Course(String id, String name) {
super();
this.id = id;
this.name = name;
}
#XmlID
#XmlAttribute(name = "id")
public String getId() {
return id;
}
#XmlAttribute(name = "name")
public String getName() {
return name;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
I want to serialize the Student class using JaxB annotations to something like the following:
<Student name="David">
<courses>
<Course id="1" name="Fundamentals of Programming"/>
<Course id="2" name="Advanced Programming">
</courses>
</Student>
Note that I want courses field of the Student to be HashMap and when unmarshalling the serialized Object use id as the key for each Course.
You can use an XmlAdapter for this use case. An XmlAdapter allows you to programmatically convert an object to another type for the purpose of marshalling/unmarshalling. For your use case you will convert the HashMap to an instance of an object that has a collection of Course.
http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html

Resources