Byron Kiourtzoglou

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

XML Binding with Simple Framework

In this tutorial we are going to see how can we Simple Framewrok for XML Binding in Android.

The basic steps to create a Customized dialog are:

  • Create or download the XML formated file you want to bind it to Android.
  • Create the appropriate classes to map appropriately the XML elements and its corresponding attributes
  • Use the appropriate annotations, e.g. @Root, @Element, @Attribute, @ElementList that reflect the nature of each field
  • Use appropriate annotation parameters, like (name="Query"), (required ="true"), alongside annotations to provide corresponding XML properties of the field
  • Use Simple Framework to read the source XML file, create instances of the class you’ve declared and populate them with the corresponding values from the XML file.

Let’s see how the code looks like:

package com.javacodegeeks.xml.bind;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.javacodegeeks.xml.bind.model.OpenSearchDescription;

public class SimpleExampleActivity extends Activity {

    private static final String url = 

  "http://dl.dropbox.com/u/7215751/JavaCodeGeeks/AndroidFullAppTutorialPart03/Transformers+2007.xml";

    private DefaultHttpClient client = new DefaultHttpClient();

    @Override
    public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  try {

String xmlData = retrieve(url);

Serializer serializer = new Persister();

Reader reader = new StringReader(xmlData);

OpenSearchDescription osd = 

    serializer.read(OpenSearchDescription.class, reader, false);

Log.d(SimpleExampleActivity.class.getSimpleName(), osd.toString());

  } 

  catch (Exception e) {

Toast.makeText(this, "Error Occured", Toast.LENGTH_LONG).show();

  }

    }

    public String retrieve(String url) {

  HttpGet getRequest = new HttpGet(url);

  try {

HttpResponse getResponse = client.execute(getRequest);

final int statusCode = getResponse.getStatusLine().getStatusCode();

if (statusCode != HttpStatus.SC_OK) {

    return null;

}

HttpEntity getResponseEntity = getResponse.getEntity();

if (getResponseEntity != null) {

    return EntityUtils.toString(getResponseEntity);

}

  } 

  catch (IOException e) {

getRequest.abort();

Log.w(getClass().getSimpleName(), "Error for URL " + url, e);

  }

  return null;

    }

}
package com.javacodegeeks.xml.bind.model;

import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root
public class OpenSearchDescription {

 @Element(name="Query")
 public Query query;

 @Element
 public int totalResults;

 @ElementList
 public List<Movie> movies;

}
package com.javacodegeeks.xml.bind.model;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;

@Element
public class Query {

 @Attribute
 public String searchTerms;

}
package com.javacodegeeks.xml.bind.model;

import java.util.List;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;

@Element(name="movie")
public class Movie {

 @Element(required=false)
 public String score;

 @Element(required=false)
 public String popularity;

 @Element(required=false)
 public String name;

 @Element(required=false)
 public String id;

 @Element(required=false)
 public String biography;

 @Element(required=false)
 public String url;

 @Element(required=false)
 public String version;

 @Element(required=false)
 public String lastModifiedAt;

 @ElementList
 public List<Image> images;

}
package com.javacodegeeks.xml.bind.model;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;

@Element(name="image")
public class Image {

 @Attribute
 public String type;

 @Attribute
 public String url;

 @Attribute
 public String size;

 @Attribute
 public int width;

 @Attribute
 public int height;

 @Attribute
 public String id;

}

 
This was an example of how to use Simple Framework to perform XML Binding in your Android Application.

Related Article:

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close