Are there any StAX implementation for android?
Asked Answered
K

2

7

I want to use StAX API implementation in android 1.6 and above devices. Are there any implementations out there ? I cannot use the jar file directly since it gives issues regarding inner class. If its not available, is there any way I can recompile the implementation ? Is there an alternate way for POJO class to be mapped into XML and vice versa directly, please exclude SAX parser and DOM parser.

I think it is possible for POJO class to be mapped into XML and vice versa using JAXB. But the situation is like this. Consider this example,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cars>
<car registration="abc123">
    <brand>BMW</brand>
    <description>Sedan</description>
</car>
<car registration="abc123">
    <brand>Ferrari</brand>
    <description>SportsCar</description>
</car>
</cars>

Now in the result I want List which has the 2 cars in it.

Also how does JAXB parser fare against StAX ?

Kitchenmaid answered 21/4, 2011 at 0:48 Comment(4)
what about this thread? A guy is referring to StaxMate which seems to utilise Woodstox Whats wrong with the built in SAX parser though?Amarette
My main purpose is to map POJOs into the XML and vice versa. If there is any direct and MORE EFFICIENT way to do this, I am all to it.Kitchenmaid
I think built-in SAX parser is not a true XML parser (the way Xerces that standard Java SE JDK uses) but rather a subset based on XPP; which means no support for DTD or some of other advanced features. Sometimes that matters, although often not. Other possibility is API convenience; Stax is often more convenient to use than SAX. However, JAXB can be used on both SAX and Stax sources; so for data binding SAX might work fine.Abiding
JAXB runs on top of either SAX or Stax (or if you must, DOM, but that's just wasteful). So it adds some overhead on top of parsing; however, when used properly this overhead is not huge; maybe +50% over it. But you must avoid reconstructing main factory objects, and use fastest XML parsers available (Sun's Sjsxp for example is bit slow)Abiding
E
4

So what you really want to "map POJOs into the XML and vice versa" is the Simple XML Library. You can use it with every version of Android from 1.5 up.

I even wrote a blog post explaining how to include it in one of your projects.

Emerick answered 21/4, 2011 at 4:50 Comment(10)
Thanks. It looks good. How does it fare against StAX, SAX, JAXB 2.0 purely in terms of performance and memory ?Kitchenmaid
Well I don't know. I don't have performance needs. It uses whichever framework it can behind the scenes so it should be only just slower if it is slower at all. Experimentally it seems really fast.Emerick
I implemented the XML Pull parser that comes with android and made it generic so that any class can use it. So that I did not need external library.Kitchenmaid
@Erigami Thankyou! Link fixed!Emerick
Well I can't see anything simple in "Simple XML Library", it's not difficult to implement but is not as "on the go" as the name suggest, yo need to create many public classes and use somewhat non-intuitive annotations to read a somwehat-simple XML. I only wanted to retrieve some nested value and I needed to create all the xml structure in order to get to it.Damicke
@htafoya: Simple XML is designed to be used for people who want so serialise and deserialise entire bodies of XML. For it's purpose it is perfect. However, if you want to extract single values from XML then what you want is XQuery: mxquery.org/?page_id=252Emerick
Blog link is dead.Clausen
Good call @free6om. Fixed.Emerick
Are people still using Simple, I thought I read that it was no longer under development anymore?Horseplay
I've actually just swapped to JSON for everything; where possible.Emerick
A
8

As far as I know, both Woodstox and Aalto should work on Android. Aalto is the single fastest conforming XML parser on Java platform, if that matters; and Woodstox supports widest range of XML constructs (from full DTD handling to RelaxNG/XML Schema validation).

For binding POJOs to XML, you could also consider Jackson extension jackson-xml-databind: while Jackson is mainly JSON processor, extension supports JAXB-style data binding for XML. And does it faster than JAXB reference implementation (see jvm-serializers benchmark). This also should work on Android (Jackson itself is nr 1 JSON parser on Android).

Abiding answered 28/5, 2011 at 17:39 Comment(4)
Thanks for that info Staxman. Until now I've only been using the standard SAX parser. Through this info I'm off reading about Aalto. It claims to be very fast, so i assume this equates to efficiency and thus better battery usage on the android. CheersAmarette
Ah, by "standard" you probably mean "one bundled with JDK", which for Sun J2SE JDK is Sun's Sjsxp. It's ok (very slow for writing XML, alas), and there is just one Stax impl you really need to avoid, which is the reference implementation (RI, by BEA, one that "stax.codehaus.org" had). As to Aalto, yeah, it would seem like a good fit. It bit smaller jar than Woodstox, and for many use cases twice as fast too.Abiding
Ahh thats interesting, yeah i want one much faster. The app i'm building now is heavily reliant on XML parsing for several different tabs and lists. At the moment its the XML parsing which is the slowest part. Actually Stax, xerxes, and aalto all seem like a better solution to what i currently have. Btw, by standard im using these imports for my xml parsing where i implement my own XML handler (import org.xml.sax.XMLReader, javax.xml.parsers.SAXParser, javax.xml.parsers.SAXParserFactory) Are those the ones your referring to? as Sun's Sjsxp? Thanks for your advice mateAmarette
Those would be for using SAX parser (first is from SAX API; second JAXP 'wrapper' to create SAX parser instance). Sun's sjsxp is default thing that implements Stax API pieces (javax.xml.stream.*).Abiding
E
4

So what you really want to "map POJOs into the XML and vice versa" is the Simple XML Library. You can use it with every version of Android from 1.5 up.

I even wrote a blog post explaining how to include it in one of your projects.

Emerick answered 21/4, 2011 at 4:50 Comment(10)
Thanks. It looks good. How does it fare against StAX, SAX, JAXB 2.0 purely in terms of performance and memory ?Kitchenmaid
Well I don't know. I don't have performance needs. It uses whichever framework it can behind the scenes so it should be only just slower if it is slower at all. Experimentally it seems really fast.Emerick
I implemented the XML Pull parser that comes with android and made it generic so that any class can use it. So that I did not need external library.Kitchenmaid
@Erigami Thankyou! Link fixed!Emerick
Well I can't see anything simple in "Simple XML Library", it's not difficult to implement but is not as "on the go" as the name suggest, yo need to create many public classes and use somewhat non-intuitive annotations to read a somwehat-simple XML. I only wanted to retrieve some nested value and I needed to create all the xml structure in order to get to it.Damicke
@htafoya: Simple XML is designed to be used for people who want so serialise and deserialise entire bodies of XML. For it's purpose it is perfect. However, if you want to extract single values from XML then what you want is XQuery: mxquery.org/?page_id=252Emerick
Blog link is dead.Clausen
Good call @free6om. Fixed.Emerick
Are people still using Simple, I thought I read that it was no longer under development anymore?Horseplay
I've actually just swapped to JSON for everything; where possible.Emerick

© 2022 - 2024 — McMap. All rights reserved.