jaxb collection

Hi,

I am trying below example

http://www.thejavageek.com/2013/09/13/jaxb-and-collections/

package com.thejavageek.jaxb;

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 com.thejavageek.jaxb;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

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 com.thejavageek.jaxb;

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

'
eclipse complaining in bike class saying color not valid etc. please advise how to fix this
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.

gurpsbassiCommented:
paste the exact error please.
gudii9Author Commented:
it shows as attached.

eclispe also shows error with spaces also as in the attachment which is bit weird
err11.png
gurpsbassiCommented:
I copied the Bike class into my IDE exactly as you have pasted it.
I do not get any errors. Its fine.

Try compiling from command line. You will then at least know its an IDE issue.
Also try doing a project clean.

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
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

gudii9Author Commented:
not sure suddenly why IDE acts weird. Does IDE act weird time to time like this?
Have you ever seen this kind of issue with eclipse IDE.
gurpsbassiCommented:
Yes IDE's can get itself in a state sometimes.
Please confirm you were able to compile from command line.
gudii9Author Commented:
through command line when i gave command as below

javac Bike.java
i see 100 errors as below

áá á}
 ^
Bike.java:22: illegal character: \160
áá á}
   ^
Bike.java:26: illegal character: \160
áá á}
^
Bike.java:26: illegal character: \160
áá á}
 ^
Bike.java:26: illegal character: \160
áá á}
   ^
Bike.java:31: illegal character: \160
áá á}
^
Bike.java:31: illegal character: \160
áá á}
 ^
Bike.java:31: illegal character: \160
áá á}
   ^
Bike.java:33: illegal character: \160
áá ápublic int getEngineCapacity() {
^
Bike.java:33: illegal character: \160
áá ápublic int getEngineCapacity() {
 ^
Bike.java:33: illegal character: \160
áá ápublic int getEngineCapacity() {
   ^
Bike.java:33: <identifier> expected
áá ápublic int getEngineCapacity() {
    ^
Bike.java:33: ';' expected
áá ápublic int getEngineCapacity() {
          ^
Bike.java:33: invalid method declaration; return type required
áá ápublic int getEngineCapacity() {
               ^
Bike.java:34: illegal character: \160
áá ááá áreturn engineCapacity;
^
Bike.java:34: illegal character: \160
áá ááá áreturn engineCapacity;
 ^
Bike.java:34: illegal character: \160
áá ááá áreturn engineCapacity;
   ^
Bike.java:34: illegal character: \160
áá ááá áreturn engineCapacity;
    ^
Bike.java:34: illegal character: \160
áá ááá áreturn engineCapacity;
     ^
Bike.java:34: illegal character: \160
áá ááá áreturn engineCapacity;
       ^
Bike.java:34: ';' expected
áá ááá áreturn engineCapacity;
        ^
Bike.java:34: not a statement
áá ááá áreturn engineCapacity;
               ^
Bike.java:35: illegal character: \160
áá á}
^
Bike.java:35: illegal character: \160
áá á}
 ^
Bike.java:35: illegal character: \160
áá á}
   ^
Bike.java:37: illegal character: \160
áá á@XmlElement
^
Bike.java:37: illegal character: \160
áá á@XmlElement
 ^
Bike.java:37: illegal character: \160
áá á@XmlElement
   ^
Bike.java:37: <identifier> expected
áá á@XmlElement
    ^
Bike.java:37: ';' expected
áá á@XmlElement
     ^
Bike.java:38: illegal character: \160
áá ápublic void setEngineCapacity(int engineCapacity) {
^
Bike.java:38: illegal character: \160
áá ápublic void setEngineCapacity(int engineCapacity) {
 ^
Bike.java:38: illegal character: \160
áá ápublic void setEngineCapacity(int engineCapacity) {
   ^
Bike.java:39: illegal character: \160
áá ááá áthis.engineCapacity = engineCapacity;
^
Bike.java:39: illegal character: \160
áá ááá áthis.engineCapacity = engineCapacity;
 ^
Bike.java:39: illegal character: \160
áá ááá áthis.engineCapacity = engineCapacity;
   ^
Bike.java:39: illegal character: \160
áá ááá áthis.engineCapacity = engineCapacity;
    ^
Bike.java:39: illegal character: \160
áá ááá áthis.engineCapacity = engineCapacity;
     ^
Bike.java:39: illegal character: \160
áá ááá áthis.engineCapacity = engineCapacity;
       ^
Bike.java:39: ';' expected
áá ááá áthis.engineCapacity = engineCapacity;
        ^
Bike.java:39: illegal start of expression
áá ááá áthis.engineCapacity = engineCapacity;
            ^
Bike.java:40: illegal character: \160
áá á}
^
Bike.java:40: illegal character: \160
áá á}
 ^
Bike.java:40: illegal character: \160
áá á}
   ^
Bike.java:42: illegal character: \160
áá ápublic int getVin() {
^
Bike.java:42: illegal character: \160
áá ápublic int getVin() {
 ^
Bike.java:42: illegal character: \160
áá ápublic int getVin() {
   ^
Bike.java:42: <identifier> expected
áá ápublic int getVin() {
    ^
Bike.java:42: ';' expected
áá ápublic int getVin() {
          ^
Bike.java:42: invalid method declaration; return type required
áá ápublic int getVin() {
               ^
Bike.java:43: illegal character: \160
áá ááá áreturn vin;
^
Bike.java:43: illegal character: \160
áá ááá áreturn vin;
 ^
Bike.java:43: illegal character: \160
áá ááá áreturn vin;
   ^
Bike.java:43: illegal character: \160
áá ááá áreturn vin;
    ^
Bike.java:43: illegal character: \160
áá ááá áreturn vin;
     ^
Bike.java:43: illegal character: \160
áá ááá áreturn vin;
       ^
Bike.java:43: ';' expected
áá ááá áreturn vin;
        ^
Bike.java:43: not a statement
áá ááá áreturn vin;
               ^
Bike.java:44: illegal character: \160
áá á}
^
Bike.java:44: illegal character: \160
áá á}
 ^
Bike.java:44: illegal character: \160
áá á}
   ^
Bike.java:46: illegal character: \160
áá á@XmlAttribute
^
Bike.java:46: illegal character: \160
áá á@XmlAttribute
 ^
Bike.java:46: illegal character: \160
áá á@XmlAttribute
   ^
Bike.java:46: <identifier> expected
áá á@XmlAttribute
    ^
Bike.java:46: ';' expected
áá á@XmlAttribute
     ^
Bike.java:47: illegal character: \160
áá ápublic void setVin(int vin) {
^
Bike.java:47: illegal character: \160
áá ápublic void setVin(int vin) {
 ^
Bike.java:47: illegal character: \160
áá ápublic void setVin(int vin) {
   ^
Bike.java:48: illegal character: \160
áá ááá áthis.vin = vin;
^
Bike.java:48: illegal character: \160
áá ááá áthis.vin = vin;
 ^
Bike.java:48: illegal character: \160
áá ááá áthis.vin = vin;
   ^
Bike.java:48: illegal character: \160
áá ááá áthis.vin = vin;
    ^
Bike.java:48: illegal character: \160
áá ááá áthis.vin = vin;
     ^
Bike.java:48: illegal character: \160
áá ááá áthis.vin = vin;
       ^
Bike.java:48: ';' expected
áá ááá áthis.vin = vin;
        ^
Bike.java:48: illegal start of expression
áá ááá áthis.vin = vin;
            ^
Bike.java:49: illegal character: \160
áá á}
^
Bike.java:49: illegal character: \160
áá á}
 ^
Bike.java:49: illegal character: \160
áá á}
   ^
Bike.java:51: illegal character: \160
áá ápublic String toString() {
^
Bike.java:51: illegal character: \160
áá ápublic String toString() {
 ^
Bike.java:51: illegal character: \160
áá ápublic String toString() {
   ^
Bike.java:51: <identifier> expected
áá ápublic String toString() {
    ^
Bike.java:51: ';' expected
áá ápublic String toString() {
          ^
Bike.java:51: invalid method declaration; return type required
áá ápublic String toString() {
                  ^
Bike.java:52: illegal character: \160
áá ááá áreturn "Bike [color=" + color + ", engineCapacity=" + engineCapacity
^
Bike.java:52: illegal character: \160
áá ááá áreturn "Bike [color=" + color + ", engineCapacity=" + engineCapacity
 ^
Bike.java:52: illegal character: \160
áá ááá áreturn "Bike [color=" + color + ", engineCapacity=" + engineCapacity
   ^
Bike.java:52: illegal character: \160
áá ááá áreturn "Bike [color=" + color + ", engineCapacity=" + engineCapacity
    ^
Bike.java:52: illegal character: \160
áá ááá áreturn "Bike [color=" + color + ", engineCapacity=" + engineCapacity
     ^
Bike.java:52: illegal character: \160
áá ááá áreturn "Bike [color=" + color + ", engineCapacity=" + engineCapacity
       ^
Bike.java:52: ';' expected
áá ááá áreturn "Bike [color=" + color + ", engineCapacity=" + engineCapacity
        ^
Bike.java:53: illegal character: \160
áá ááá ááá ááá á+ ", vin=" + vin + "]";
^
Bike.java:52: not a statement
áá ááá áreturn "Bike [color=" + color + ", engineCapacity=" + engineCapacity
                                                            ^
Bike.java:53: illegal character: \160
áá ááá ááá ááá á+ ", vin=" + vin + "]";
 ^
Bike.java:53: illegal character: \160
áá ááá ááá ááá á+ ", vin=" + vin + "]";
   ^
Bike.java:53: illegal character: \160
áá ááá ááá ááá á+ ", vin=" + vin + "]";
    ^
Bike.java:53: illegal character: \160
áá ááá ááá ááá á+ ", vin=" + vin + "]";
     ^
100 errors

errors.png
gudii9Author Commented:
package com.thejavageek.jaxb;

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


when i removed every single space in front eclipse i happy.

When i ran the  test program i got below output
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]


But i wonder why the XML did not printed as mentioned in the link.

This will generate the xml output as follows


<?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>

where can i see below XML??

            File showRoomDump = new File("./showRoom.xml");
 i do not think it got created or created but hidden from me?
gudii9Author Commented:
@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;
      }

what is meaning of above code. ShowRoom.java class has 3 variables name, id and bike(which is complex vaiable of arraylist or something???)
Please advise

Overall what are we doing here

Marshalling

Create a JAXBContext for ShowRoomclass using JAXBContext context = JAXBContext.newInstance(ShowRoom.class);.
Create a Marshaller from context using Marshaller jaxbMarshaller = context.createMarshaller();
Use jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); so that the generated xml is formatted and easy to read.
Finally marshal the ShowRoomusing jaxbMarshaller.marshal(showRoom, showRoomDump); where showRoomDumpis the file object created using File showRoom = new File("./showRoomDump.xml");
 

Unmarshalling

Create Unmarshaller from the context created previously using Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
Unmarshal the xml file into ShowRoomobject using ShowRoom unMarshalledShowRoom = (ShowRoom) jaxbUnmarshaller.unmarshal(showRoomDump);
So we will write a JAXBTest class to try it out as follows
gudii9Author Commented:
'open new window java' option copying somewhat good. I still need to remove extra numbers at top
gurpsbassiCommented:
where can i see below XML??

            File showRoomDump = new File("./showRoom.xml");

Change it to a path you're comfortable with e.g. "C:\\showRoom.xml"
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.