Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

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?
Avatar of gudii9

ASKER

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

have you tried jaxbMarshaller.marshal(ar, System.out); ?
Avatar of gudii9

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);

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)
Ahhh, sorry yes of course that would not work.

You need to create a custom wrapper type for your arraylist.
Avatar of gudii9

ASKER

why and how to co the custom wrapping for my arraylist 'ar' in this case
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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

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);
Avatar of gudii9

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);

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]
Avatar of gudii9

ASKER

<?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
Avatar of gudii9

ASKER

System.out.println(unMarshalledBike);

above is wrong
Avatar of gudii9

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);

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>
I thought that was obvious.
Avatar of gudii9

ASKER

i see marshalled xml output.

How to see unmarshalled object output at the end?
please advise
Avatar of gudii9

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);

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?
Avatar of gudii9

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</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
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?
Avatar of gudii9

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);

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

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.
Avatar of gudii9

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.
Avatar of gudii9

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>

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)
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.
Avatar of gudii9

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);

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
which one of the above files is bikeDump.xml and bikeDumpTest.xml?
Avatar of gudii9

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>

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

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.
Avatar of gudii9

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</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?
If the bikedumptest contains multiple bikes in a list, you need the wrapper element around it.
Avatar of gudii9

ASKER

sure let me try