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
Related
Are there alternatives to Jersey 1.8 that offer converting XML files to java.util.List? In my web service lists are converted to XML and then sent to the client.
The problem with Jersey is that converting XML files will not work because Android different folders ignored in the libraries.
I would be grateful for any help.
If you don't have many XML responses with different structures, you could just create a method that parses the XML for example with XPath or Java DOM. You can also take a look at XStream which is a great alternative to JAXB.
If you don't have to stick to XML, you can also switch to JSON: JSON Support - Jersey . I've personally found Jackson to be simplier than other solutions.
I am working on a project in Android now that involves parsing XML from a local file.
I have done XML work extensiely in flex and have become used to the power and flexibility of E4X and it's capabilities. I wonder now if I can do something similar in android or will I be forced to parse the XML manually?
Thanks
Quoting #Andreas_D:
E4X is a language extension, XML is treated like a primitive. E4X is not just for parsing XML, it's using XML as real types.
This can't be simulated or done with a Java 'framework', it would require a language extension for Java.
And, since Android does not have JAXB, you don't have that, either.
You have your choice of DOM, SAX, and the XmlPullParser, plus any third-party Java libraries you can find that have been ported to Android and fit whatever size constraints you may have.
check this out-
Working with XML on Android
Would you please tell me about any good third party xml parsing library that can be used in Android
thanks
Simple XML is just WONDERFUL. You can use SAX or DOM which come in the official SDK, but compared to SimpleXML they are too complex (again: compared to Simple XML).
You can use SAX or JDOM. I believe SAX is a bit faster than JDOM. You can find more information about SAX on Wikipedia: http://en.wikipedia.org/wiki/Simple_API_for_XML
Also, here's a link to SAX on Android and class references from android.com: http://developer.android.com/reference/android/sax/package-summary.html
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.
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