marshal unmarshal

Hi,
I am following below link
package MarshalUnmarshal;
 
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");
 
		File bikeDump = new File("./bikeDump.xml");
 
		try {
			JAXBContext context = JAXBContext.newInstance(Bike.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(hondaCBR, bikeDump);
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			Bike unMarshalledBike = (Bike) jaxbUnmarshaller.unmarshal(bikeDump);
			System.out.println(unMarshalledBike);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}

Open in new window


package MarshalUnmarshal;
 
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


where below xml created
"./bikeDump.xml"

How to see above XML in the console. I wonder why that is not displayed on console as mentioned in the link.

control+shifr+R --->not showing above XML

when i went to windows explorer to that project folder i see xml as below


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike>


why we have only one XmlAttribute?

  @XmlAttribute
    public void setVin(int vin) {
        this.vin = vin;
    }

how to add more bikes

package MarshalUnmarshal;
 
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 hondaCBR2 = new Bike(25631249, 25000, "white");
 
		File bikeDump = new File("./bikeDump.xml");
 
		try {
			JAXBContext context = JAXBContext.newInstance(Bike.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(hondaCBR, bikeDump);
			jaxbMarshaller.marshal(hondaCBR2, bikeDump);
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			Bike unMarshalledBike = (Bike) jaxbUnmarshaller.unmarshal(bikeDump);
			System.out.println(unMarshalledBike);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}

Open in new window


instead of adding one other white bike apart from black bike it overridden previous pne as below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike vin="25631249">
    <color>white</color>
    <engineCapacity>25000</engineCapacity>
</bike>

Open in new window


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.

gurpsbassiCommented:
I am assuming your talking about your IDE and Ctrl shift R. You need to refresh Your project if you're looking for a new file on disk.
gurpsbassiCommented:
.why we have only one XmlAttribute?  

Ummm surely that's all up to you. Make another attribute if u want.
mccarlIT Business Systems Analyst / Software DeveloperCommented:
How to see above XML in the console.

Try changing the below line...

jaxbMarshaller.marshal(hondaCBR, bikeDump);

to this...

jaxbMarshaller.marshal(hondaCBR, System.out);



how to add more bikes

Try adding each bike object to an ArrayList and then marshalling the ArrayList. The way you tried basically creates the XML file for the black bike and then immediately overwrites that XML file with the XML for the white bike.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

gudii9Author Commented:
why i was not able to see created XML in the package or project explorer of the eclipse. please advise
gurpsbassiCommented:
did you refresh your project? Right click > refresh?
gudii9Author Commented:
refresh worked. printing to console worked.
how to marshal arraylist
package marshal;
 
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 hondaCBR2 = new Bike(25631249, 25000, "white");
		ArrayList ar=new ArrayList();
		ar.add(hondaCBR);
		ar.add(hondaCBR2);
 
		File bikeDump = new File("./bikeDump.xml");
 
		try {
			JAXBContext context = JAXBContext.newInstance(Bike.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(hondaCBR, bikeDump);
			jaxbMarshaller.marshal(hondaCBR, System.out);
			//jaxbMarshaller.marshal(hondaCBR2, bikeDump);
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			Bike unMarshalledBike = (Bike) jaxbUnmarshaller.unmarshal(bikeDump);
			System.out.println(unMarshalledBike);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}
//jaxbMarshaller.marshal(hondaCBR, System.out);

Open in new window

gurpsbassiCommented:
have you tried jaxbMarshaller.marshal(ar, System.out); ?
gudii9Author Commented:
package marshal;
 
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 hondaCBR2 = new Bike(25631249, 25000, "white");
		ArrayList ar=new ArrayList();
		ar.add(hondaCBR);
		ar.add(hondaCBR2);
 
		File bikeDump = new File("./bikeDump.xml");
 
		try {
			JAXBContext context = JAXBContext.newInstance(Bike.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(hondaCBR, bikeDump);
			jaxbMarshaller.marshal(hondaCBR, System.out);
			//jaxbMarshaller.marshal(hondaCBR2, bikeDump);
			System.out.println("----------------------");
			jaxbMarshaller.marshal(ar, System.out); 
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			Bike unMarshalledBike = (Bike) jaxbUnmarshaller.unmarshal(bikeDump);
			System.out.println(unMarshalledBike);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}
//jaxbMarshaller.marshal(hondaCBR, System.out);

Open in new window


when i try i am getting below error. please advise
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike>
----------------------
javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.
      at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:585)
      at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:485)
      at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:320)
      at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:248)
      at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:74)
      at marshal.JAXBTest.main(JAXBTest.java:35)
gurpsbassiCommented:
Ahhh, sorry yes of course that would not work.

You need to create a custom wrapper type for your arraylist.
gudii9Author Commented:
why and how to co the custom wrapping for my arraylist 'ar' in this case
gurpsbassiCommented:
Why? because ArrayList is a type not known to the JAXB context.

How?

1. create a BikeList class to act as a wrapper

import java.util.List;

import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "bikeCatalog")
public class BikeList {

    private List<Bike> bikes;

    @XmlElementWrapper(name = "bikeList")
    public List<Bike> getBikes() {
	return bikes;
    }

    public void setBikes(final List<Bike> bikes) {
	this.bikes = bikes;
    }

}

Open in new window


2.Register it with the JaxB context in the JAXBTest class

final JAXBContext context = JAXBContext.newInstance(
		    BikeList.class);

Open in new window

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
gudii9Author Commented:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike>
----------------------
javax.xml.bind.JAXBException: class java.util.ArrayList nor any of its super class is known to this context.
      at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:585)
      at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:485)
      at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:320)
      at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:248)
      at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:74)
      at MarshalUnmarshal.JAXBTest.main(JAXBTest.java:35)
still get error

package MarshalUnmarshal;
 
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 MarshalUnmarshal;
import java.util.List;

import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "bikeCatalog")
public class BikeList {

    private List<Bike> bikes;

    @XmlElementWrapper(name = "bikeList")
    public List<Bike> getBikes() {
	return bikes;
    }

    public void setBikes(final List<Bike> bikes) {
	this.bikes = bikes;
    }

}

Open in new window


package MarshalUnmarshal;
 
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 hondaCBR2 = new Bike(25631249, 25000, "white");
		ArrayList ar=new ArrayList();
		ar.add(hondaCBR);
		ar.add(hondaCBR2);
 
		File bikeDump = new File("./bikeDump.xml");
 
		try {
			//JAXBContext context = JAXBContext.newInstance(Bike.class);
			final JAXBContext context = JAXBContext.newInstance(BikeList.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(hondaCBR, bikeDump);
			jaxbMarshaller.marshal(hondaCBR, System.out);
			//jaxbMarshaller.marshal(hondaCBR2, bikeDump);
			System.out.println("----------------------");
			jaxbMarshaller.marshal(ar, System.out); 
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			Bike unMarshalledBike = (Bike) jaxbUnmarshaller.unmarshal(bikeDump);
			System.out.println(unMarshalledBike);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}
//jaxbMarshaller.marshal(hondaCBR, System.out);

Open in new window

gurpsbassiCommented:
Thats because you're still instantiating an ArrayList in your Test class.
You need to instantiate the BikeList class instead and set the List on it using the setter method.

e.g BikeList bl = new BikeList();
      bl.setBikes(al);

then jaxbMarshaller.marshal(bl, System.out);
gudii9Author Commented:
package MarshalUnmarshal;
 
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 hondaCBR2 = new Bike(25631249, 25000, "white");
		
		
	/*	Thats because you're still instantiating an ArrayList in your Test class.
		You need to instantiate the BikeList class instead and set the List on it using the setter method.

		e.g BikeList bl = new BikeList();
		      bl.setBikes(al);

		then jaxbMarshaller.marshal(bl, System.out);*/
		
		try {
		ArrayList ar=new ArrayList();
		ar.add(hondaCBR);
		ar.add(hondaCBR2);
		BikeList bl = new BikeList();
	      bl.setBikes(ar);
		File bikeDump = new File("./bikeDump.xml");
 
		
			//JAXBContext context = JAXBContext.newInstance(Bike.class);
			final JAXBContext context = JAXBContext.newInstance(BikeList.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(hondaCBR, bikeDump);
			jaxbMarshaller.marshal(hondaCBR, System.out);
			//jaxbMarshaller.marshal(hondaCBR2, bikeDump);
			System.out.println("----------------------");
			jaxbMarshaller.marshal(bl, System.out); 
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			Bike unMarshalledBike = (Bike) jaxbUnmarshaller.unmarshal(bikeDump);
			System.out.println(unMarshalledBike);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}
//jaxbMarshaller.marshal(hondaCBR, System.out);

Open in new window


now i got
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike>
----------------------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
Bike [color=black, engineCapacity=250, vin=25631248]
gudii9Author Commented:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike>
above is still wrong .

Below output is correct. please advise
gudii9Author Commented:
System.out.println(unMarshalledBike);

above is wrong
gudii9Author Commented:
package MarshalUnmarshal;
 
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 hondaCBR2 = new Bike(25631249, 25000, "white");
		
		
	/*	Thats because you're still instantiating an ArrayList in your Test class.
		You need to instantiate the BikeList class instead and set the List on it using the setter method.

		e.g BikeList bl = new BikeList();
		      bl.setBikes(al);

		then jaxbMarshaller.marshal(bl, System.out);*/
		
		try {
		ArrayList ar=new ArrayList();
		ar.add(hondaCBR);
		ar.add(hondaCBR2);
		BikeList bl = new BikeList();
	      bl.setBikes(ar);
		File bikeDump = new File("./bikeDump.xml");
 
		
			//JAXBContext context = JAXBContext.newInstance(Bike.class);
			final JAXBContext context = JAXBContext.newInstance(BikeList.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(hondaCBR, bikeDump);
			jaxbMarshaller.marshal(bl, System.out);
			//jaxbMarshaller.marshal(hondaCBR2, bikeDump);
			System.out.println("----------------------");
			jaxbMarshaller.marshal(bl, System.out); 
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			Bike unMarshalledBike = (Bike) jaxbUnmarshaller.unmarshal(bikeDump);
			//System.out.println(bl);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}
//jaxbMarshaller.marshal(hondaCBR, System.out);

Open in new window


above gave expected output..i have to change to bl
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
----------------------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
gurpsbassiCommented:
I thought that was obvious.
gudii9Author Commented:
i see marshalled xml output.

How to see unmarshalled object output at the end?
please advise
gudii9Author Commented:
package MarshalUnmarshal;
 
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 hondaCBR2 = new Bike(25631249, 25000, "white");
		
		
	/*	Thats because you're still instantiating an ArrayList in your Test class.
		You need to instantiate the BikeList class instead and set the List on it using the setter method.

		e.g BikeList bl = new BikeList();
		      bl.setBikes(al);

		then jaxbMarshaller.marshal(bl, System.out);*/
		
		try {
		ArrayList ar=new ArrayList();
		ar.add(hondaCBR);
		ar.add(hondaCBR2);
		BikeList bl = new BikeList();
	      bl.setBikes(ar);
		File bikeDump = new File("./bikeDump.xml");
 
		
			//JAXBContext context = JAXBContext.newInstance(Bike.class);
			final JAXBContext context = JAXBContext.newInstance(BikeList.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(hondaCBR, bikeDump);
			jaxbMarshaller.marshal(bl, System.out);
			//jaxbMarshaller.marshal(hondaCBR2, bikeDump);
			System.out.println("----------------------marshalled xml output from given object");
			jaxbMarshaller.marshal(bl, System.out); 
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			Bike unMarshalledBike = (Bike) jaxbUnmarshaller.unmarshal(bikeDump);
			System.out.println("----------------------unmarshal object output from given xml");
			System.out.println(unMarshalledBike);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}
//jaxbMarshaller.marshal(hondaCBR, System.out);

Open in new window


i wrote above
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
----------------------marshalled xml output from given object
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
----------------------unmarshal object output from given xml
Bike [color=black, engineCapacity=250, vin=25631248]

i got like above .

i expected to see both bike objects in output i got only one. How to get other bike too?


----------------------unmarshal object output from given xml
Bike [color=black, engineCapacity=250, vin=25631248]


what is custom wrapper object? which scenario we have to use? is it is similar to XSD?
gudii9Author Commented:
@XmlRootElement(name = "bike123")
public class Bike {
 

i changed to bike123 instead of bike still i got same output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
----------------------marshalled xml output from given object
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
----------------------unmarshal object output from given xml
Bike [color=black, engineCapacity=250, vin=25631248]


what is the significance of that name above. i thought it should map and fail when i said bike123.

please advise
gurpsbassiCommented:
I have no idea what you are trying to acheive.
You have a line of code that is in marshalling a single bike object and yet you are expecting it to give you a list of bikes?
gudii9Author Commented:
package MarshalUnmarshal;
 
import java.io.File;
 
public class JAXBTest {
 
	public static void main(String[] args) {
 
		Bike hondaCBR = new Bike(25631248, 250, "black");
		Bike hondaCBR2 = new Bike(25631249, 25000, "white");
		
		
	/*	Thats because you're still instantiating an ArrayList in your Test class.
		You need to instantiate the BikeList class instead and set the List on it using the setter method.

		e.g BikeList bl = new BikeList();
		      bl.setBikes(al);

		then jaxbMarshaller.marshal(bl, System.out);*/
		
		try {
		ArrayList ar=new ArrayList();
		ar.add(hondaCBR);
		ar.add(hondaCBR2);
		BikeList bl = new BikeList();
	      bl.setBikes(ar);
	      File bikeDump = new File("./bikeDump.xml");
	      File bikeDumpTest = new File("./bikeDumpTest.xml");
 
		
			//JAXBContext context = JAXBContext.newInstance(Bike.class);
			final JAXBContext context = JAXBContext.newInstance(BikeList.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(hondaCBR, bikeDump);
			jaxbMarshaller.marshal(bl, System.out);
			//jaxbMarshaller.marshal(hondaCBR2, bikeDump);
			System.out.println("----------------------marshalled xml output from given object");
			jaxbMarshaller.marshal(bl, System.out); 
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			Bike unMarshalledBike = (Bike) jaxbUnmarshaller.unmarshal(bikeDumpTest);
			System.out.println("----------------------unmarshal object output from given xml");
			System.out.println(unMarshalledBike);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}
//jaxbMarshaller.marshal(hondaCBR, System.out);

Open in new window

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike1234 vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike1234>
<bike1234 vin="2563124899">
    <color>white</color>
    <engineCapacity>25099</engineCapacity>
</bike1234>

Open in new window


i added one other child tag to the xml so that when i unmarshall i can see along with black car white car also.But getting error as below. please advise
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
----------------------marshalled xml output from given object
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"bike1234"). Expected elements are <{}bike123>,<{}bikeCatalog>
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:659)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:255)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:250)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:117)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1060)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:495)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:477)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:147)
	at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
	at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
	at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:221)
	at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:193)
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:136)
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:141)
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:150)
	at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:168)
	at MarshalUnmarshal.JAXBTest.main(JAXBTest.java:51)

Open in new window

gurpsbassiCommented:
What the heck is a bike1234 doing there?

You can't just make up any old xml and expect java to work magically.

You need to annotate your object model to let it know what bike1234 is.
gudii9Author Commented:
i want to marshal to one xml. I want to unmarshall from different xml with more than one child tag (white car tag and other black car tag). When i unmarshall i want to see both white and black car on the console. But i got above error not sure how to resolve it.
gudii9Author Commented:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike>
<bike vin="2563124899">
    <color>white</color>
    <engineCapacity>25099</engineCapacity>
</bike>

Open in new window


i remeoved 1234 as above still getting error as below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
----------------------marshalled xml output from given object
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"bike"). Expected elements are <{}bike123>,<{}bikeCatalog>
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:659)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:255)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:250)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:117)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1060)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:495)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:477)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:147)
      at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
      at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
      at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
      at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
      at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
      at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
      at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
      at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
      at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
      at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:221)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:193)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:136)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:141)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:150)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:168)
      at MarshalUnmarshal.JAXBTest.main(JAXBTest.java:51)
gurpsbassiCommented:
You have totally lost me.

When making a change, please post your java objects and your xml file each time so I know what you have done.
gudii9Author Commented:
package MarshalUnmarshal;
 
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 hondaCBR2 = new Bike(25631249, 25000, "white");
		
		
	/*	Thats because you're still instantiating an ArrayList in your Test class.
		You need to instantiate the BikeList class instead and set the List on it using the setter method.

		e.g BikeList bl = new BikeList();
		      bl.setBikes(al);

		then jaxbMarshaller.marshal(bl, System.out);*/
		
		try {
		ArrayList ar=new ArrayList();
		ar.add(hondaCBR);
		ar.add(hondaCBR2);
		BikeList bl = new BikeList();
	      bl.setBikes(ar);
	      File bikeDump = new File("./bikeDump.xml");
	      File bikeDumpTest = new File("./bikeDumpTest.xml");
 
		
			//JAXBContext context = JAXBContext.newInstance(Bike.class);
			final JAXBContext context = JAXBContext.newInstance(BikeList.class);
			Marshaller jaxbMarshaller = context.createMarshaller();
 
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
			jaxbMarshaller.marshal(hondaCBR, bikeDump);
			jaxbMarshaller.marshal(bl, System.out);
			//jaxbMarshaller.marshal(hondaCBR2, bikeDump);
			System.out.println("----------------------marshalled xml output from given object");
			jaxbMarshaller.marshal(bl, System.out); 
 
			Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
			Bike unMarshalledBike = (Bike) jaxbUnmarshaller.unmarshal(bikeDumpTest);
			System.out.println("----------------------unmarshal object output from given xml");
			System.out.println(unMarshalledBike);
 
		} catch (JAXBException e) {
			e.printStackTrace();
		}
 
	}
 
}
//jaxbMarshaller.marshal(hondaCBR, System.out);

Open in new window


package MarshalUnmarshal;
import java.util.List;

import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "bikeCatalog")
public class BikeList {

    private List<Bike> bikes;

    @XmlElementWrapper(name = "bikeList")
    public List<Bike> getBikes() {
	return bikes;
    }

    public void setBikes(final List<Bike> bikes) {
	this.bikes = bikes;
    }

}

Open in new window


package MarshalUnmarshal;
 
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement(name = "bike123")
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


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike>
<bike vin="2563124899">
    <color>white</color>
    <engineCapacity>25099</engineCapacity>
</bike>

Open in new window


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike123 vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike123>

Open in new window


i am marshalling from java collection object and putting into bikeDump.xml and unmarshalling bikeDumpTest.xml to java object
gurpsbassiCommented:
which one of the above files is bikeDump.xml and bikeDumpTest.xml?
gudii9Author Commented:
bikeDumpTest.xml is
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike>
<bike vin="2563124899">
    <color>white</color>
    <engineCapacity>25099</engineCapacity>
</bike>

Open in new window

bikeDump.xml is
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike123 vin="25631248">
    <color>black</color>
    <engineCapacity>250</engineCapacity>
</bike123>

Open in new window

gurpsbassiCommented:
Right well, the problem is obvious.

How can you expect to unmarshall bikedumptest.xml when it contains <bike> elements.
You have told JAXB the root node is
@XmlRootElement(name = "bike123")

You cant martial using one root node and unmarshall using another.
gudii9Author Commented:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
----------------------marshalled xml output from given object
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
    <bikeList>
        <bikes vin="25631248">
            <color>black</color>
            <engineCapacity>250</engineCapacity>
        </bikes>
        <bikes vin="25631249">
            <color>white</color>
            <engineCapacity>25000</engineCapacity>
        </bikes>
    </bikeList>
</bikeCatalog>
javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.]
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:314)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:544)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:227)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:193)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:136)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:141)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:150)
      at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:168)
      at MarshalUnmarshal.JAXBTest.main(JAXBTest.java:51)
Caused by: org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
      at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
      at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
      at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
      at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
      at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
      at org.apache.xerces.impl.XMLDocumentScannerImpl$TrailingMiscDispatcher.dispatch(Unknown Source)
      at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
      at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
      at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
      at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
      at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
      at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:221)
      ... 6 more

i removed 123 from Bike.java as below and getting error as above
package MarshalUnmarshal;
 
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


does Bike.java and BikeList.java used both in marshal and unmarshal scenario?
gurpsbassiCommented:
If the bikedumptest contains multiple bikes in a list, you need the wrapper element around it.
gudii9Author Commented:
sure let me try
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 EE

From novice to tech pro — start learning today.