Home » Android » core » xml » XML Binding with Simple Framework

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. 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 our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close