Alternate XML parser implementations for Android
I am looking for an XML parsing solution for Android besides the built-in kXML pull parser. I am trying to parse a large (4MB+) XML file downloaded from a server and the kXML parser throws an OutOfMemory error after trying to allocate a 1MB+ byte array while parsing. A good streaming XML parser shouldn't be allocating such a big array!!
The kXML v2 parser being used in Android appears to be about 7 years old. I'd like to try something else to see if it fixes my problem, ideally something that implements the same org.xmlpull.v1 interface since my code is already written against it. Woodstox looks like a good choi开发者_StackOverflow社区ce and it has an XMLPullParser adapter but apparently the Android engineers are making it difficult to include the javax.xml.streaming libraries. I see some rather involved "jar-jar" workarounds but I'd rather not spend a bunch of time messing around with Ant if there is an easier way. Has anyone else already gotten another XML pull parser working and can point me to it?
In a worst case scenario, you might revert to SAX which is the most memory efficient approach. Though it is a big change in your code I guess.
The best solution I've found so far is an old Javolution library that contains an XmlPullParser. The Javolution pull parser doesn't actually implement org.xmlpull.v1.XmlPullParser but it has the exact same method signatures except that its methods return CharSequence instead of String. This was easily remedied by wrapping it into a homemade adapter.
Unfortunately it seems that the Javolution pull parser has been deprecated and cannot be found in recent versions of the JAR but I found an older JAR file that still has it here: http://download.osgeo.org/webdav/geotools/org/javolution/javolution/3.7.1/
Also it turns out my problem was that my XML file happened to contain over 1MB of 0x00 chars at the end of it, which are not legal characters in the first place!! However if anyone wants to try another parser I hope this helps you.
I am planning to use the source code for a StAX
implementation you can download here. I just posted a question here about it. There are other implementations. like Woodstox
.
I use libgdx library and it has a XmlReader class implemented very nice!
Just copy it and remove this method parse(FileHandle file)
code link: https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/utils/XmlReader.java
also use ObjectMap
imported.
精彩评论