I had been using Simple Api for quite some time now to marshal and UN-marshal xml files to Java classes on Android. While I tried my hand with XPP, Spring Android but I found Simple to be absolutely simple!! It works like a charm. Interestingly, Spring also uses Simple Api. It has support for posts like atom, rss feeds. But all these can be done using only Simple as well. Here is the link for Simple. The code examples are easy to follow. All one needs to do is to put the jar into Android (or any java project) project's classpath.
In addition to all the serialization/de-serialization magic that it performs,one thing that I like about Simple is the ability to write the data from the class into an OutputStream. For e.g:
I do not appreciate putting a try/catch block with a generic "Exception" but that is how the api is written. It mandates you to catch a generic Exception. Hope they will modify that later.
It can be used to generate the xml in the form of a String:
One can also write the contents to a file instead of a OutputStream.
But I just love Simple...somebody had once said, "Simple living and high thinking"....I can attribute this to Simple api..."Simple parsing and high performing"!
In addition to all the serialization/de-serialization magic that it performs,one thing that I like about Simple is the ability to write the data from the class into an OutputStream. For e.g:
//MyXml is the class
MyXml myXml = new MyXml();
myXml.description = "some_desc";
myXml.name = "some_name";
//assuming MyXml has already been defined the way the xml is going to be parsed, //with it's root, elements, attributes.
Serializer serializer = new Persister();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
serializer.write(myXml, bos);
}catch(Exception e) {//whatever}
I do not appreciate putting a try/catch block with a generic "Exception" but that is how the api is written. It mandates you to catch a generic Exception. Hope they will modify that later.
It can be used to generate the xml in the form of a String:
String xmlString = new String(bos.toByteArray(), "UTF-8");
This String can be sent as a data to some server over Http from Android, if the need be.One can also write the contents to a file instead of a OutputStream.
File file = new File(Environment.getExternalStorageDirectory()
+ "/some.xml");
serializer.write(myXml, file);
Again that should be inside a try/catch...ah what an innovative way to catch Exception... pun intended!!But I just love Simple...somebody had once said, "Simple living and high thinking"....I can attribute this to Simple api..."Simple parsing and high performing"!
can post your full source code here?
ReplyDeleteI have already provided a link in my post, where you can get very illustrative sample code.
ReplyDeletehttp://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php
That alone should suffice. :)
If you still need some more code, then send me your mail id. I will try to send you when I am little free. Till then explore the tutorial.
Happy coding!