Tuesday, 8 May 2012

XML parsing with Simple library


Hello Guys, XML parsing in Android is little bit boring task and if the XML is complex and lengthy then sometimes it is headache to parse the XML. Today I found a Simple library for parse the XML response.Using Simple library you do not have to parse the XM:L the library will automatically parse the XML and fill the your class object. You only need to make a model class according to the structure of the XML file.

Model class Example.java


@Root
public class Example {

   @Element
   private String text;

   @Attribute
   private int index;

   public Example() {
      super();
   }  

   public Example(String text, int index) {
      this.text = text;
      this.index = index;
   }

   public String getMessage() {
      return text;
   }

   public int getId() {
      return index;
   }
}


Example.java is the model class with the Getter and Setter methods.In order to serialize an object to XML a series of annotations must be placed within that object. These annotations tell the persister how the object should be serialized. For example take the class shown below. Here there are three different annotations, one used to describe the name of the root element, one that describes an XML message element, and a final annotation for an id attribute.

Serializing Example class object


Serializer serializer = new Persister();
Example example = new Example("Example message", 123);
File result = new File("example.xml");

serializer.write(example, result);


To serialize an instance of the above object a Persister is required. The persister object is then given an instance of the annotated object and an output result, which is a file in this example. Other output formats are possible with the persister object.

XML after Serializing a Example class object


Once the above code is executed the object instance will have been transferred as an XML document to the specified file. The resulting XML file will contain the contents shown below.


<example index="123">
   <text>Example message</text>
</example>

As well as the capability of using the field an object name to acquire the XML element and attribute names explicit naming is possible. Each annotation contains a name attribute, which can be given a string providing the name of the XML attribute or element. This ensures that should the object have unusable field or method names they can be overridden, also if your code is obfuscated explicit naming is the only reliable way to serialize and deserialize objects consistently. An example of the previous object with explicit naming is shown below.


Deserializing a Example class object


Serializer serializer = new Persister();
File source = new File("example.xml");

Example example = serializer.read(Example.class, source);



Taking the above example object the XML deserialization process is described in the code snippet shown below. As can be seen the deserialization process is just as simple. The persister is given the class representing the serialized object and the source of the XML document. To deserialize the object the read method is used, which produces an instance of the annotated object. Also, note that there is no need to cast the return value from the read method as the method is generic.


For review more you can refer this blog.



3 comments:

  1. Thanks for the simple but useful tutorial.
    It will create a new file, right? what if i want to add some data afterwards? Help me out? :-)

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. hi can we use this for JSON too?

    ReplyDelete