I would like to serialize an object to XML inside Android.
Any libs suggested?
PS: Already tried XStream, but it doesn't serialize enums correctly with Android. The issue is here: Serialization problem with Enums at Android
Have you tried simple http://simple.sourceforge.net/ it is working well for me, correctly handles enums, the jar is quite large though 320k.
Unfortunately I don't think there is a good solution for xml serialization of objects in Android yet. Most of the existing solutions are too heavyweight for use in mobile apps, and depend on things Android doesn't support.
For an overview of XML options, see working with xml in Android.
It's not XML, but you might want to use Google's GSON library to serialize objects. It's what they recommend in the their documentation as an alternative to Serializable:
JSON is concise, human-readable and efficient. Android includes both a streaming API and a tree API to read and write JSON. Use a binding library like GSON to read and write Java objects directly.
Related
I have a SOAP Web Service that i want to parse and use in my android project. I have read that there is a website that auto generates the java classes by importing the url (wsl2code) but it seems to have flaws. Additionally the sample they provide is not working. What is the best and correct way of generating java classes from a WSDL url for my android project?
Use ksoal2 library for soap service which is light weight for android but the think you have to create service call manually
To answer my own question, I have used the service of wsdl2code http://www.wsdl2code.com/pages/home.aspx . Just some information regarding that, it has some limitations like
Don't Support byref Variables , in class
Don't Support Boolean& Variables , in class
Don't Support Int64& Variables , in class
Methods that are boolean or a custom class are turned to void and must be modified manually
Overall though this website saved me since it would take me a while to create the classes I needed by myself. It might have taken me a while to modify the classes but still saved a lot of time.
In my Android app, I need to talk to a webserver that expects marshalled objects of complex classes. These custom classes were produces from a WSDL file, so they already come with annotations. The server uses JAXB for marhalling and unmarhalling.
So, on the client side, using JAXB, I would simple to this:
JAXBContext jc = JAXBContext.newInstance("schema_definition_path");
Marshaller m = jc.createMarshaller();
Unmarshaller u = jc.createUnmarshaller();
...and then use the Marshaller and Unmarshaller instances to work serialize/deserialize the custom objects.
Since, it's not a great idea to use the heavy-weight JAXB lib in mobile apps, I am looking for an alternative to do what JAXB does for me here.
I checked out Simple XML Serialization, but that doesn't seem to provide this kind of functionality. I can only give a class to the unmarshaller instead of the schema definition path. Doing this results in an incomplete xml document, containing only the root element.
Can anyone point me in the right direction, please? Is this even possible? Should I use a different lib - which one? I am I just misusing SimpleXML?
Since I couldn't find any better way (see comments to the original question), I decided to manually convert the Objects using the XML Pull API implementation included in Android.
Here is a short tutorial on how to use it: http://www.ibm.com/developerworks/opensource/library/x-android/
I'm not saying it's a good way, but it's the only thing I can think of that will work and where I don't have to touch the original classes.
I created a xsd and I created Java objects using Castor. Then I imported this in my Android project and tried to build an XML from the object, using the marshal method.
Marshaller.marshal(v, writer);
I received some wired exception trouble processing "javax/xml/parsers/DocumentBuilder.class":...
So my question is Can I use Castor inside Android, If yes how can I work around this.
If no, what is the neat solution available to convert object to xml and xml to object in Android, similar to Castor.
Thanks in Advance.
I personally do not have any experience with Castor but from the website it would seem that it has dependencies which android does not support and therefore cannot be used inside android.
I ended up using Simple xml from simple.sourceforge.net/home.php. This looks light suitable for Android. But this will not create Java objects for your xsd. You have to create them on your own. Is there a way to build these objects directly from xsd compantible with Android?
I have to parse some complex xml files inside my Android application. Is there any good library for doing that like there is TouchXMl for iPhone?
Or you could use the org.xmlpull.v1.XmlPullParser - I've found it much easier to use than the SAX Parser and it has other benefits:
http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html
http://www.bearcave.com/software/java/xml/xmlpull.html
The SAX XML Parser comes already built into the Android SDK.
Use Woodstox. It's a Stax pull parser, actually supports all of XML (unlike xmlpull that is bundled), efficient, mature. For more convenience, StaxMate helps too, uses Woodstox (or any other Stax parser) for parsing but exposes convenient cursors and writers; but is still 3x - 5x faster than DOM approach.
XmlPullParser that was mentioned works to some degree, but can't handle DTDs (not just validation but even basic character entities), gives cryptic error messages and doesn't have standard interface (SAX or Stax).
Don't know the criteria by which "best" is defined. but here is a recent research paper that analyzes existing java xml framework with great details...
http://recipp.ipp.pt/bitstream/10400.22/1847/1/ART_BrunoOliveira_2013.pdf
A REST XML (not JSON!) Web Service should exchange XML Schema specified XML between a Google App Engine and an Android app.
I wanted to use XStream for both, however, I could not get it to work for the Google App Engine, therefore to me Apache XMLBeans is the next best choice (JAXB does not work on both).
However, with Google App Engine there is no problem, but on Android, I get several severe exceptions (eg. due to the usage of the Stax API with its javax.xml.* packages).
So,
Is there any other XML-binding possibility to stream XML documents on GAE and Android?
If not, is it possible to patch Apache XMLBeans to work with Android?
Thanks!
I'm poking in the dark here, since i haven't tried anything of this, yet:
There's this blog entry from XBinder which claims that they are releasing an android-compatible version "in a few weeks". While that might not be an option right now, they also explain a bit of how they have done it, wrapping a light StAX-like wrapper on the XmlPull support already present in Android.
(my answer originally had another paragraph on XStream working on android, but then i read the question again and saw that your problem was with getting XStream to work on the AppEngine side...)
An XML data binding framework that works on both Google App Engine and Android is Simple. It uses annotations similar to JAXB to annotate a POJO which can then be serialized and deserialized in XML. For example, an annotated object would look like.
#Root
public class Pojo {
#Attribute
private String name;
#Element
private String value
public String getName() {
return name;
}
public String getValue() {
return value;
}
}
Then to generate XML all you have to do is.
Serializer serializer = new Persister();
serializer.write(instance, outputSteam)
To read you can do
Serializer serializer = new Persister();
Pojo pojo = serializer.read(Pojo.class, inputSteam)
And thats it! Its quite a powerful framework with many other features and annotations. It works for Android, Google App Engine, and any JDK 1.5+. It has no dependencies and is very light weight. For more information see the Tutorial.
Another option is Pulloid (pulloid.org). It relies on the XmlPull API which is included in Android. On the App Engine side you would need to use an XmlPull implementation such as XPP3 for now - I don't know if it's a show stopper or not.
Android lacks built-in support for XML generation, period. Android is stronger with JSON, since it can both parse and generate JSON documents.
In terms of patching XMLBeans, you may find it quicker to find some other package that has fewer dependencies. You cannot readily import any new code that resides in the java.* or javax.* packages.
You can use Castor . Just be sure, in Android 2.1, not to use default android SAXParser. You'll get namespace errors. You do this by defining the parser to be, for example, Xerces (and the you add the required JARS), in core.properties . In android 2.2 it may be ok. Note that if you create an xmlcontext for the unmarsheler with xerces, it still won't work, as the mapping itself would be parsed with android's SAX. It must be done at core (top level properties file) so that even the mapping is parsed by xerces. finally - performance is as slow as you can expect... :( Good luck SM