Android: XML parsing thoughts, part 2
Senin, 17 Oktober 2011
0
komentar
This is the second part of the article about XML parsing. The first part of it is available here.
In this post I want to tell you about one 3rd party XML handling lib that you may want to use in Android development.
It's Simple framework. Simple is a high performance XML serialization and configuration framework originally from Java world. But it can be used in Android too. It offers full object serialization and deserialization, maintaining each reference encountered.
So, what is the feature of Simple? Simple framework uses annotations to describe correspondence between Java objects and their XML representation in a declarative manner. As a result we have very demonstrable Java code without weird boilerplate stuff! :)
To use Simple in your Android project just add .jar file as a library (Project properties -> Java Build Path -> Libraries tab). Now you are ready! For example we need to parse XML like this:
No problems, it's easy! Just create Java class that describes ninja:
Finally deserialization is pretty simple:
In this post I want to tell you about one 3rd party XML handling lib that you may want to use in Android development.
It's Simple framework. Simple is a high performance XML serialization and configuration framework originally from Java world. But it can be used in Android too. It offers full object serialization and deserialization, maintaining each reference encountered.
So, what is the feature of Simple? Simple framework uses annotations to describe correspondence between Java objects and their XML representation in a declarative manner. As a result we have very demonstrable Java code without weird boilerplate stuff! :)
To use Simple in your Android project just add .jar file as a library (Project properties -> Java Build Path -> Libraries tab). Now you are ready! For example we need to parse XML like this:
<ninja>
<name>Katakuna</name>
<weapon type="melee">sword</weapon>
</ninja>
No problems, it's easy! Just create Java class that describes ninja:
Looks very demonstrable, isn't it? ;)@Root(name="ninja")
public class Ninja
{
@Element(name="name")
public String name;
@Element(name="weapon")
public String weapon;
@Attribute(name="type")
public String weaponType;
}
Finally deserialization is pretty simple:
I think this small example has intrigued you with Simple framework! You can learn more about Simple framework features (which are so cool!) at tutorials and articles pages. Download it here (it's free and licensed under Apache license).Reader reader = new StringReader(xmlStream);
Persister serializer = new Persister();
try
{
Ninja ninja = serializer.read(Ninja.class, reader, false);
}
catch (Exception e)
{
Log.e("XML with Simple demo", e.getMessage());
}
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Android: XML parsing thoughts, part 2
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://androidillegal.blogspot.com/2011/10/android-xml-parsing-thoughts-part-2.html. Terima kasih sudah singgah membaca artikel ini.Ditulis oleh Unknown
Rating Blog 5 dari 5
0 komentar:
Posting Komentar