Marshalling a document using JIBX

Just as you can create objects out of XML, JIBX allows you to create XML out of objects too. The magic of JIBX is that it allows as many has-a relationships within a hierarchal structure. I made just the following changes to the class. And it worked like a charm.

The MattizMain with changes from the previous example:

package com.mattiz.main;

import java.util.ArrayList;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.JiBXException;
import com.mattiz.vo.Author;
import com.mattiz.vo.Book;

public class MattizMain {
	public void convertToXMl() {
		try {
			Author author = new Author();
			author.setName("Mattiz Cuppajava");
			Book book1 = new Book();
			book1.setTitle("The follies of Old Age");

			Book book2 = new Book();
			book2.setTitle("Ethical shoplifting");

			Book book3 = new Book();
			book3.setTitle("The purdah and beyond");

			Book book4 = new Book();
			book4.setTitle("Tales by Hashmi");

			ArrayList bookslist = new ArrayList();
			String mattizXml = null;
			// Marshall document using Jibx
			IBindingFactory bindingFactory = BindingDirectory
			IMarshallingContext iMarshallingContext = bindingFactory
			ByteArrayOutputStream authorXmlOutputStream = new ByteArrayOutputStream();
			iMarshallingContext.marshalDocument(author, "UTF-8", null,
			mattizXml = authorXmlOutputStream.toString();
		} catch (JiBXException jibxe) {

	public static void main(String args[]) {
		MattizMain mattizMain = new MattizMain();

Here is the output XML from the sys out.

<?xml version="1.0" encoding="UTF-8"?>
<Author xmlns="">
        <Title>The follies of Old Age</Title>
        <Title>Ethical shoplifting</Title>
        <Title>The purdah and beyond</Title>
        <Title>Tales by Hashmi</Title>
    <Name>Mattiz Cuppajava</Name>

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: