Good alternative for JAXBContext to use in Android? - android

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.

Related

Is it possible to reliably modify a java string inside the C++ NDK directly (without making a copy)?

The only way I see to do it is getting a reference to do jstring as described here, then GetStringCritical, and using it as a u16string (so any other strings acting upon it should be u16string as well).
But I'm not sure that will work since it doesn't seem certain that GetStringCritical will return a pointer to the original (it's possible it will create a copy).
The posts I found about this are relatively old, I'm wondering whether there is a better way to do this, or maybe now all modern platforms do not create a copy when using GetStringCritical?
Strings in Java are immutable by design. They can't change, and finding some hack to do so via C would probably break things if possible.

Why do we use xml language for layouts? [duplicate]

I would like to know why we use XML for the creating user interface layouts in Android. I know that it decouples the business logic from the design but what is the significance of the XML other than that?
Also I would like to know the significance of the auto-generated R.java file in this. All I know that it is generated according to the changes in the resources and that it helps us to access the widgets and resources through their IDs.
It would be great if someone could give a clear idea on these two aspects.
Unlike what everyone said about the XML being easy and efficient. Here is what I read in Hello Android by Ed Brunnette (p. 49) which made sense.
Android is optimized for mobile devices with limited memory and
horsepower, so you may find it strange that it uses XML so
pervasively. After all, XML is a verbose, human-readable format not
known for its brevity or efficiency, right?
Although you see XML when writing your program, the Eclipse plug-in
invokes the Android resource compiler, aapt, to preprocess the XML
into a compressed binary format.**It is this format, not the original
XML text, that is stored on the device.
This was the kind of answer that i was looking for.(sorry if my question meant otherwise).
The reason that XML was chosen is mainly because of its familiarity and the number of IDE tools that natively support it. The developers could have chosen JSON for example and still compiled that to binary.The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource.
XML is easy to parse and manipulate programmatically, it's basically a tree structure and most UI creation tools already use it. It really has nothing to do with decoupling business logic because you can define Java code in Android using a Model-View-Controller pattern just as well.
The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource. It also stops you from making stupid mistakes since the compiler will complain if you try to access a resource you haven't defined. If you were using a simple properties file you wouldn't know until runtime that the 'key' you are using is missing.
Same as why is silverlight with xml the answer is simple xml give power by integration and scalability. R.java is for indexing having things organized is never bad.
Sorry my english
One possible reason is that you need not have any working java underneath in order to be able to see the visual layout of the interface you are working on. The xml ui element/page is essentially a document that you can parse and display. If this were a source file you would have to either carefully parse it or compile and run it (all of which are more complex than parsing xml)
Xml as itself is well readable both by human and machine. Also, it is scalable and simple to develop. Also, you have already mentioned the decoupling.
As for R.java - it is just the way of accessing widgets. The solution is rather good, as it is using ints instead of string to use less memory and in the same time provides well readable names for the simplicity of development.
Android Layouts are tree like structures with some enforced rules. XMLs are perfect fit for this purpose.
JSON also have tree like structure but they are data-oriented while XML is document-oriented.** :
Meaning XML is based on the idea that documents have structural and
other semantic elements that can be described without reference to
how such elements should be displayed.
The actual display of such a document may vary, depending on the
output medium and style preferences.
While JSON was designed to
represent JavaScript and their prime purpose is data exchange . They are well suited in data-oriented areas
because of light weight and simplicity & closer in syntactic form to programming data structures.

Android Converting objects to XML and vice versa

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?

Android XML Object Serialization

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.

Android and Google App Engine: supported XML Binding Tool available?

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

Categories

Resources