gudii9
asked on
marshal unmarshal
Hi,
I am following below link
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</engin eCapacity>
</bike>
why we have only one XmlAttribute?
@XmlAttribute
public void setVin(int vin) {
this.vin = vin;
}
how to add more bikes
instead of adding one other white bike apart from black bike it overridden previous pne as below
please advise
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();
}
}
}
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 + "]";
}
}
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</engin
</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();
}
}
}
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>
please advise
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.
.why we have only one XmlAttribute?
Ummm surely that's all up to you. Make another attribute if u want.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
why i was not able to see created XML in the package or project explorer of the eclipse. please advise
did you refresh your project? Right click > refresh?
ASKER
refresh worked. printing to console worked.
how to marshal arraylist
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);
have you tried jaxbMarshaller.marshal(ar, System.out); ?
ASKER
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);
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 .JAXBConte xtImpl.get BeanInfo(J AXBContext Impl.java: 585)
at com.sun.xml.internal.bind.v2.runtime .XMLSerial izer.child AsRoot(XML Serializer .java:485)
at com.sun.xml.internal.bind.v2.runtime .Marshalle rImpl.writ e(Marshall erImpl.jav a:320)
at com.sun.xml.internal.bind.v2.runtime .Marshalle rImpl.mars hal(Marsha llerImpl.j ava:248)
at javax.xml.bind.helpers.AbstractMarsh allerImpl. marshal(Ab stractMars hallerImpl .java:74)
at marshal.JAXBTest.main(JAXBTest.java: 35)
Ahhh, sorry yes of course that would not work.
You need to create a custom wrapper type for your arraylist.
You need to create a custom wrapper type for your arraylist.
ASKER
why and how to co the custom wrapping for my arraylist 'ar' in this case
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>still get error
<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 .JAXBConte xtImpl.get BeanInfo(J AXBContext Impl.java: 585)
at com.sun.xml.internal.bind.v2.runtime .XMLSerial izer.child AsRoot(XML Serializer .java:485)
at com.sun.xml.internal.bind.v2.runtime .Marshalle rImpl.writ e(Marshall erImpl.jav a:320)
at com.sun.xml.internal.bind.v2.runtime .Marshalle rImpl.mars hal(Marsha llerImpl.j ava:248)
at javax.xml.bind.helpers.AbstractMarsh allerImpl. marshal(Ab stractMars hallerImpl .java:74)
at MarshalUnmarshal.JAXBTest.main(JAXBT est.java:3 5)
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 + "]";
}
}
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;
}
}
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);
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);
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,
ASKER
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);
now i got
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike vin="25631248">
<color>black</color>
<engineCapacity>250</engin
</bike>
----------------------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
<bikeList>
<bikes vin="25631248">
<color>black</color>
<engineCapacity>250</engin
</bikes>
<bikes vin="25631249">
<color>white</color>
<engineCapacity>25000</eng
</bikes>
</bikeList>
</bikeCatalog>
Bike [color=black, engineCapacity=250, vin=25631248]
ASKER
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>above is still wrong .
<bike vin="25631248">
<color>black</color>
<engineCapacity>250</engineCapacity>
</bike>
Below output is correct. please advise
ASKER
System.out.println(unMarshalledBike) ;
above is wrong
ASKER
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);
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</engin
</bikes>
<bikes vin="25631249">
<color>white</color>
<engineCapacity>25000</eng
</bikes>
</bikeList>
</bikeCatalog>
----------------------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
<bikeList>
<bikes vin="25631248">
<color>black</color>
<engineCapacity>250</engin
</bikes>
<bikes vin="25631249">
<color>white</color>
<engineCapacity>25000</eng
</bikes>
</bikeList>
</bikeCatalog>
I thought that was obvious.
ASKER
i see marshalled xml output.
How to see unmarshalled object output at the end?
please advise
How to see unmarshalled object output at the end?
please advise
ASKER
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);
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</engineCapacit y>
</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</engineCapacit y>
</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?
----------------------unma
Bike [color=black, engineCapacity=250, vin=25631248]
what is custom wrapper object? which scenario we have to use? is it is similar to XSD?
ASKER
@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</engin
</bikes>
<bikes vin="25631249">
<color>white</color>
<engineCapacity>25000</eng
</bikes>
</bikeList>
</bikeCatalog>
----------------------mars
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bikeCatalog>
<bikeList>
<bikes vin="25631248">
<color>black</color>
<engineCapacity>250</engin
</bikes>
<bikes vin="25631249">
<color>white</color>
<engineCapacity>25000</eng
</bikes>
</bikeList>
</bikeCatalog>
----------------------unma
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
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?
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?
ASKER
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);
<?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>
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)
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.
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.
ASKER
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.
ASKER
<?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>
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</engineCapacit y>
</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</engineCapacit y>
</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 .unmarshal ler.Unmars hallingCon text.handl eEvent(Unm arshalling Context.ja va:659)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Loader .reportErr or(Loader. java:255)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Loader .reportErr or(Loader. java:250)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Loader .reportUne xpectedChi ldElement( Loader.jav a:117)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Unmars hallingCon text$Defau ltRootLoad er.childEl ement(Unma rshallingC ontext.jav a:1060)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Unmars hallingCon text._star tElement(U nmarshalli ngContext. java:495)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Unmars hallingCon text.start Element(Un marshallin gContext.j ava:477)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.SAXCon nector.sta rtElement( SAXConnect or.java:14 7)
at org.apache.xerces.parsers.AbstractSA XParser.st artElement (Unknown Source)
at org.apache.xerces.impl.XMLNSDocument ScannerImp l.scanStar tElement(U nknown Source)
at org.apache.xerces.impl.XMLNSDocument ScannerImp l$NSConten tDispatche r.scanRoot ElementHoo k(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFr agmentScan nerImpl$Fr agmentCont entDispatc her.dispat ch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFr agmentScan nerImpl.sc anDocument (Unknown Source)
at org.apache.xerces.parsers.XML11Confi guration.p arse(Unkno wn Source)
at org.apache.xerces.parsers.XML11Confi guration.p arse(Unkno wn Source)
at org.apache.xerces.parsers.XMLParser. parse(Unkn own Source)
at org.apache.xerces.parsers.AbstractSA XParser.pa rse(Unknow n Source)
at org.apache.xerces.jaxp.SAXParserImpl $JAXPSAXPa rser.parse (Unknown Source)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Unmars hallerImpl .unmarshal 0(Unmarsha llerImpl.j ava:221)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Unmars hallerImpl .unmarshal (Unmarshal lerImpl.ja va:193)
at javax.xml.bind.helpers.AbstractUnmar shallerImp l.unmarsha l(Abstract Unmarshall erImpl.jav a:136)
at javax.xml.bind.helpers.AbstractUnmar shallerImp l.unmarsha l(Abstract Unmarshall erImpl.jav a:141)
at javax.xml.bind.helpers.AbstractUnmar shallerImp l.unmarsha l(Abstract Unmarshall erImpl.jav a:150)
at javax.xml.bind.helpers.AbstractUnmar shallerImp l.unmarsha l(Abstract Unmarshall erImpl.jav a:168)
at MarshalUnmarshal.JAXBTest.main(JAXBT est.java:5 1)
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.
When making a change, please post your java objects and your xml file each time so I know what you have done.
ASKER
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);
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;
}
}
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 + "]";
}
}
<?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>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike123 vin="25631248">
<color>black</color>
<engineCapacity>250</engineCapacity>
</bike123>
i am marshalling from java collection object and putting into bikeDump.xml and unmarshalling bikeDumpTest.xml to java object
which one of the above files is bikeDump.xml and bikeDumpTest.xml?
ASKER
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>
bikeDump.xml is<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bike123 vin="25631248">
<color>black</color>
<engineCapacity>250</engineCapacity>
</bike123>
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.
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.
ASKER
<?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</engineCapacit y>
</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</engineCapacit y>
</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.AbstractUnmar shallerImp l.createUn marshalExc eption(Abs tractUnmar shallerImp l.java:314 )
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Unmars hallerImpl .createUnm arshalExce ption(Unma rshallerIm pl.java:54 4)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Unmars hallerImpl .unmarshal 0(Unmarsha llerImpl.j ava:227)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Unmars hallerImpl .unmarshal (Unmarshal lerImpl.ja va:193)
at javax.xml.bind.helpers.AbstractUnmar shallerImp l.unmarsha l(Abstract Unmarshall erImpl.jav a:136)
at javax.xml.bind.helpers.AbstractUnmar shallerImp l.unmarsha l(Abstract Unmarshall erImpl.jav a:141)
at javax.xml.bind.helpers.AbstractUnmar shallerImp l.unmarsha l(Abstract Unmarshall erImpl.jav a:150)
at javax.xml.bind.helpers.AbstractUnmar shallerImp l.unmarsha l(Abstract Unmarshall erImpl.jav a:168)
at MarshalUnmarshal.JAXBTest.main(JAXBT est.java:5 1)
Caused by: org.xml.sax.SAXParseException: The markup in the document following the root element must be well-formed.
at org.apache.xerces.util.ErrorHandlerW rapper.cre ateSAXPars eException (Unknown Source)
at org.apache.xerces.util.ErrorHandlerW rapper.fat alError(Un known Source)
at org.apache.xerces.impl.XMLErrorRepor ter.report Error(Unkn own Source)
at org.apache.xerces.impl.XMLErrorRepor ter.report Error(Unkn own Source)
at org.apache.xerces.impl.XMLScanner.re portFatalE rror(Unkno wn Source)
at org.apache.xerces.impl.XMLDocumentSc annerImpl$ TrailingMi scDispatch er.dispatc h(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFr agmentScan nerImpl.sc anDocument (Unknown Source)
at org.apache.xerces.parsers.XML11Confi guration.p arse(Unkno wn Source)
at org.apache.xerces.parsers.XML11Confi guration.p arse(Unkno wn Source)
at org.apache.xerces.parsers.XMLParser. parse(Unkn own Source)
at org.apache.xerces.parsers.AbstractSA XParser.pa rse(Unknow n Source)
at org.apache.xerces.jaxp.SAXParserImpl $JAXPSAXPa rser.parse (Unknown Source)
at com.sun.xml.internal.bind.v2.runtime .unmarshal ler.Unmars hallerImpl .unmarshal 0(Unmarsha llerImpl.j ava: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 + "]";
}
}
does Bike.java and BikeList.java used both in marshal and unmarshal scenario?
If the bikedumptest contains multiple bikes in a list, you need the wrapper element around it.
ASKER
sure let me try