In this howto I will show you how to create a XML Schema from an annotated JAXB class using the maven jaxb-schemagen plugin.

STEP 1:  Annotate your domain classes with JAXB annotations.

In our example we have a simple Contact class with name, surname, birthday and a list of mails.

public class Contact {
        private String name;
        private String surname;
        private Birthday birthday;
        private List<Mail> mail;
       // Add getters & setters

And the Birthday class:

public class Birthday {
        private String day;
        private String month;
        private String year;
        // Add getters & setters

And the Mail class:

public class Mail {
        public enum MailType { PERSONAL, WORK };
        private String email;
        private MailType type;
        // Add getters & setters

STEP 2: Annotate the XML namespace as a package annotation


@XmlSchema (
                xmlns = {},
                namespace = "",
                elementFormDefault = XmlNsForm.UNSET,
                attributeFormDefault = XmlNsForm.UNSET
package net.jordeu.codesnips.xsdcreator;

import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;

STEP 3: Config Maven pom.xml
First add this repository to the pom.xml or check that you have it in your settings.xml or proxy repository.

     <name> Maven 2 Repository</name>

And add this plugin changing the “includes” section and “schemas” section according to your project name and layout.



Checkout the sample project


Some days ago I needed to pass a XML file as an URL parameter. After some googling I didn’t found anything that suited my needs and I ended coding it myself.


  • Compress a String and encode it with Base64
  • Decompress and decode a Base64 encoded and compressed string.

Decompress method

public static String decompress(String inputStr) throws UnsupportedEncodingException {

		// Base64 decode
		Base64 base64 = new Base64(-1, new byte[0], true);
		byte[] bytes = base64.decode(inputStr.getBytes("UTF-8"));

		// Inflater
		Inflater decompressor = new Inflater();

		// Create an expandable byte array to hold the decompressed data
		ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);

		// Decompress the data
		byte[] buf = new byte[1024];
		while (!decompressor.finished()) {
			try {
				int count = decompressor.inflate(buf);
				bos.write(buf, 0, count);
			} catch (DataFormatException e) {
		try {
		} catch (IOException e) {

		// Get the decompressed data
		byte[] decompressedData = bos.toByteArray();

		return new String(decompressedData, "UTF-8");

Compress method

	public static String compress(String inputStr) throws UnsupportedEncodingException {
		byte[] input = inputStr.getBytes("UTF-8");

		// Compressor with highest level of compression
		Deflater compressor = new Deflater();

		// Give the compressor the data to compress

		// Create an expandable byte array to hold the compressed data.
		// It is not necessary that the compressed data will be smaller than
		// the uncompressed data.
		ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

		// Compress the data
		byte[] buf = new byte[1024];
		while (!compressor.finished()) {
			int count = compressor.deflate(buf);
			bos.write(buf, 0, count);
		try {
		} catch (IOException e) {

		// Get the compressed data
		byte[] compressedData = bos.toByteArray();

		// Encode Base64
		Base64 base64 = new Base64(-1, new byte[0], true);
		byte[] bytes = base64.encode(compressedData);
		return new String(bytes, "UTF-8");