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 MattizMain.java class. And it worked like a charm.

The MattizMain with changes from the previous example:

package com.mattiz.main;

import java.io.ByteArrayOutputStream;
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");
			book1.setIsbn("uio32o4324");

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

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

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

			ArrayList bookslist = new ArrayList();
			bookslist.add(book1);
			bookslist.add(book2);
			bookslist.add(book3);
			bookslist.add(book4);
			author.setBookList(bookslist);
			String mattizXml = null;
			// Marshall document using Jibx
			IBindingFactory bindingFactory = BindingDirectory
					.getFactory(Author.class);
			IMarshallingContext iMarshallingContext = bindingFactory
					.createMarshallingContext();
			ByteArrayOutputStream authorXmlOutputStream = new ByteArrayOutputStream();
			iMarshallingContext.marshalDocument(author, "UTF-8", null,
					authorXmlOutputStream);
			mattizXml = authorXmlOutputStream.toString();
			System.out.println(mattizXml);
		} catch (JiBXException jibxe) {
			jibxe.printStackTrace();
		}
	}

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

Here is the output XML from the sys out.

<?xml version="1.0" encoding="UTF-8"?>
<Author xmlns="http://www.com/mattiz/vo">
    <Book>
        <Title>The follies of Old Age</Title>
        <Isbn>uio32o4324</Isbn>
    </Book>
    <Book>
        <Title>Ethical shoplifting</Title>
        <Isbn>435rdfsgf</Isbn>
    </Book>
    <Book>
        <Title>The purdah and beyond</Title>
        <Isbn>43543fdad</Isbn>
    </Book>
    <Book>
        <Title>Tales by Hashmi</Title>
        <Isbn>fdsr34r332</Isbn>
    </Book>
    <Name>Mattiz Cuppajava</Name>
</Author>

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:

WordPress.com Logo

You are commenting using your WordPress.com 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: