Java XML Serialization with JAXB

I made this code after reading an StackOverflow answer form Blaise Doughan.

http://stackoverflow.com/questions/5189690/how-to-serialize-and-de-serialize-objects-using-jaxb

Also, here is a link to his blog.
http://blog.bdoughan.com/2010/07/jaxb-xml-binding-standard.html

I’m not going to provide any explanation how any of this works but I’m providing an example of using my Student object from my prior posts.

This new class called Serialize and it that will serialized my Student object and deserialized xml. I named my method to serialized a Student object toXML() and named my method that will preform the deserialization toStudent(). I haven’t fully tested this yet so user beware if you try something like this in your own code.

package XML;

import java.io.File;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.JAXBSource;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import PoJo.Student;

public class Serialize {
	
	private StringWriter writer;
	private JAXBContext context;
	private Marshaller marshal;
	private JAXBElement<PoJo.Student> element;
	private JAXBSource source;
	
	// Quick Hack...
	public Serialize( Student student) throws JAXBException{
		this.writer = new StringWriter();
		this.context = JAXBContext.newInstance(PoJo.Student.class);
		this.element = new JAXBElement( new QName("student"), PoJo.Student.class, student);
		this.source = new JAXBSource(this.context, this.element);
	}
	
	
	public JAXBSource getSource(){
		return this.source;
	}
	
	public Marshaller getMarshaller() throws JAXBException{
		return this.context.createMarshaller();
	}
	
	public Unmarshaller getUnmarshaller() throws JAXBException{
		return this.context.createUnmarshaller();
	}
	
	public StringWriter toXML( StringWriter writer ) throws JAXBException{
		this.context.createMarshaller().marshal(this.element, writer);
		return( writer );
	}
	
	public XMLStreamWriter toXML( XMLStreamWriter writer ) throws JAXBException{
		this.context.createMarshaller().marshal(this.element, writer);
		return( writer );
	}

	
	
	public Student toStudent( JAXBSource source ) throws JAXBException{
		JAXBContext context = JAXBContext.newInstance( PoJo.Student.class);
		Unmarshaller unmarshaller = context.createUnmarshaller();
		JAXBElement<PoJo.Student> element = unmarshaller.unmarshal( source, PoJo.Student.class );
		
		return( element.getValue() );
	}
	
	public Student toStudent( XMLStreamReader reader ) throws JAXBException{
		JAXBContext context = JAXBContext.newInstance( PoJo.Student.class);
		Unmarshaller unmarshaller = context.createUnmarshaller();
		JAXBElement<PoJo.Student> element = unmarshaller.unmarshal( reader, PoJo.Student.class );
		return( element.getValue() );
	}
	
	public Student toStudent( File file ) throws JAXBException{
		JAXBContext context = JAXBContext.newInstance( PoJo.Student.class);
		Unmarshaller unmarshaller = context.createUnmarshaller();
		JAXBElement<PoJo.Student> element = (JAXBElement<Student>) unmarshaller.unmarshal( file );
		return( element.getValue() );
	}
}

Also here is my test class if you want to see how it will run

package XML;

import java.io.StringWriter;

import javax.xml.bind.JAXBException;

import PoJo.Student;

public abstract class testSerialize {

	public static void main(String[] args) {
		Student bit = new Student(1, "Bit", "Diddler");
		try {
			Serialize serialize = new Serialize(bit);
			StringWriter writer = new StringWriter();
			writer = serialize.toXML(writer);
                        System.out.println("Making a xml string from a Student object");
			System.out.println( writer.toString() );

			Student newStudent = serialize.toStudent( serialize.getSource() );
                        System.out.println("Making a Student object from source");
			System.out.print( newStudent.toString() );
			
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Sample Output:
Making a xml string from a Student object

Bit
-1.0
Diddler
0
non_student

Making a Student object from source
Invalid GPA input; Set at-1.0!
Diddler, Bit
ID: -1
Enrollment Status: non_student
GPA: -1.0

—-
End of standard out.

So as you can see the standard error message in the student object was trigger since the student already had a value of -1.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s