JAXB showroom collection

Hi,

When i ran below JAXB example

package marshalCollection;
 
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement(name = "bike")
public class Bike {
 
	String color;
	int engineCapacity;
	int vin;
 
	public Bike() {
 
	}
 
	public Bike(int vin, int engineCapacity, String color) {
		this.vin = vin;
		this.engineCapacity = engineCapacity;
		this.color = color;
	}
 
	public String getColor() {
		return color;
	}
 
	@XmlElement
	public void setColor(String color) {
		this.color = color;
	}
 
	public int getEngineCapacity() {
		return engineCapacity;
	}
 
	@XmlElement
	public void setEngineCapacity(int engineCapacity) {
		this.engineCapacity = engineCapacity;
	}
 
	public int getVin() {
		return vin;
	}
 
	@XmlAttribute
	public void setVin(int vin) {
		this.vin = vin;
	}
 
	public String toString() {
		return "Bike [color=" + color + ", engineCapacity=" + engineCapacity
				+ ", vin=" + vin + "]";
	}
 
}

Open in new window


package marshalCollection;
import java.io.File;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
 
public class JAXBTest {
 
	public static void main(String[] args) {
 
		Bike hondaCBR = new Bike(25631248, 250, "black");
		Bike pulsar = new Bike(45896253, 200, "red");
		Bike karizma = new Bike(56982351, 225, "yellow");
 
		ArrayList<Bike> bikes = new ArrayList<Bike>();
		bikes.add(hondaCBR);
		bikes.add(pulsar);
		bikes.add(karizma);
 
		ShowRoom showRoom = new ShowRoom();
		showRoom.setId("someId");
		showRoom.setName("Bikes Showroom");
		showRoom.setBikes(bikes);
 
		File showRoomDump = new File("./showRoom.xml");
 
		try {
			JAXBContext context = JAXBContext.newInstance(ShowRoom.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(showRoom, showRoomDump);
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			ShowRoom unmarshalledShowRoom = (ShowRoom) jaxbUnmarshaller
					.unmarshal(showRoomDump);
			System.out.println(unmarshalledShowRoom);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}

Open in new window


package marshalCollection;
 
import java.util.ArrayList;
 
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.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
 
@XmlRootElement
@XmlType(propOrder = { "name", "id", "bikes" })
public class ShowRoom {
 
	private ArrayList<Bike> bikes = new ArrayList<Bike>();
	private String name;
	private String id;
 
	@XmlElementWrapper(name = "bikeList")
	@XmlElement(name = "bike")
	public ArrayList<Bike> getBikes() {
		return bikes;
	}
 
	public void setBikes(ArrayList<Bike> bikes) {
		this.bikes = bikes;
	}
 
	@XmlElement
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	@XmlAttribute
	public String getId() {
		return id;
	}
 
	public void setId(String id) {
		this.id = id;
	}
 
	public String toString() {
		return "ShowRoom [bikes=" + bikes + ", name=" + name + ", id=" + id
				+ "]";
	}
 
}

Open in new window


i got below output xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<showRoom id="someId">
    <name>Bikes Showroom</name>
    <bikeList>
        <bike vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bike>
        <bike vin="45896253">
            <color>red</color>
            <engineCapacity>200</engineCapacity>
        </bike>
        <bike vin="56982351">
            <color>yellow</color>
            <engineCapacity>225</engineCapacity>
        </bike>
    </bikeList>
</showRoom>


also output in console as
ShowRoom [bikes=[Bike [color=black, engineCapacity=250, vin=25631248], Bike [color=red, engineCapacity=200, vin=45896253], Bike [color=yellow, engineCapacity=225, vin=56982351]], name=Bikes Showroom, id=someId]

why we need below
@XmlRootElement
@XmlType(propOrder = { "name", "id", "bikes" })

@XmlElementWrapper(name = "bikeList")
      @XmlElement(name = "bike")


@XmlAttribute etc both in Bike.java as well as ShowRoom.java classes. Can we just have in one place. Can we use instead on XSD to do this.

Please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gudii9Author Commented:
please advise
gudii9Author Commented:
this link has lot of useful info i was looking
http://www.java2blog.com/2013/02/jaxb-tutorial.html

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.