POJO is an acronym for Plain Old Java Object, basically a class with attributes and it’s getters and setters. The name is used to emphasize that a given object is an ordinary Java Object, not a special object. The term “POJO” is mainly used to denote a Java object which does not follow any of the major Java object models, conventions, or frameworks.

Below example reads the pojo object instance variable and generates xml tags and values in the order of variable declaration in the given pojo class. You can assign tag names for each element.


public class Student {
	String name;
	String section;
	String id;

	public Student(String name, String id, String section) {
		this.name = name;
		this.id = id;
		this.section = section;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public String getSection() {
		return section;

	public void setSection(String section) {
		this.section = section;

	public String getId() {
		return id;

	public void setId(String id) {
		this.id = id;



import java.io.IOException;
import java.io.StringWriter;
import org.xmlpull.v1.XmlSerializer;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;

public class MainActivity extends Activity {
	TextView textView;

	protected void onCreate(Bundle savedInstanceState) {
		textView = (TextView) findViewById(R.id.textView);

		Student pojo = new Student("Adam" , "100120", "Grade 6");
		// Serialization begins:
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		try {
			// start document
			serializer.startDocument("UTF-8", true);
			serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

			serializer.startTag("", "students");
			serializer.startTag("", "student");
			serializer.attribute("", "ID", pojo.getId());
			serializer.startTag("", "name");
			serializer.endTag("", "name");
			serializer.startTag("", "section");
			serializer.endTag("", "section");
			serializer.endTag("", "student");
			serializer.endTag("", "students");

			// end document.

			Log.i("Pojo to xml", writer.toString());
			textView.setText("" + writer.toString());
			// Toast.makeText(getApplicationContext(), ""+ writer.toString(), 0).show();
		} catch (IllegalArgumentException e) {
		} catch (IllegalStateException e) {
		} catch (IOException e) {


Output XML

<student ID="100120">
<section>Grade 6</section>


A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.


Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.