by
5
8
4,306
0
Top 1% !
Popular
Famous
Nice
Tagged
Easy-to-find
Specified
OpenSource
Popularity: 2323rd place

Published on:

Languagejava
LicenseMIT_X11

Card class - Example for marshalling/unmarshalling java object using JAX-B

Finally this is the Card class.
It's used the standard JAX-B annotations for specifying the XmlElement and the XmlAccessorType. Note the usage of XmlAttribute annotation to specify the Vendor ID as attribute of element Card. Moreover the XmlIDREF annotation will link the attribute vendor with the ID of Vendor class. In this way once the xml will be generated, it will show the Vendor ID for each card object.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/42434/Card-class---Example-for-marshalling-unm?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
package com.test.jaxb; import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlIDREF; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) public class Card implements Serializable { @XmlElement private CardType type; @XmlElement private String number; @XmlElement private int numOfSlots; @XmlAttribute @XmlIDREF private Vendor vendor; /** * @return the vendor */ public Vendor getVendor() { return vendor; } /** * @param vendor the vendor to set */ public void setVendor(Vendor vendor) { this.vendor = vendor; } public Card() { } /** * @return the type */ public CardType getType() { return type; } /** * @param type the type to set */ public void setType(CardType type) { this.type = type; } /** * @return the number */ public String getNumber() { return number; } /** * @param number the number to set */ public void setNumber(String number) { this.number = number; } /** * @return the numOfSlots */ public int getNumOfSlots() { return numOfSlots; } /** * @param numOfSlots the numOfSlots to set */ public void setNumOfSlots(int numOfSlots) { this.numOfSlots = numOfSlots; } /** * @param type * @param number * @param numOfSlots */ public Card(CardType type, String number, int numOfSlots, Vendor vendor) { super(); this.type = type; this.number = number; this.numOfSlots = numOfSlots; this.vendor=vendor; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Card [type=" + type + ", number=" + number + ", numOfSlots=" + numOfSlots + ", vendor=" +vendor+"]"; } }

Other Steps and Solutions

If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus