1

I need to parse a series of simple XML nodes (String format) as they arrive from a persistent socket connection. Is a custom Android SAX parser really the best way? It seams slightly overkill to do it in this way

I had naively hoped I could cast the strings to XML then reference the names / attributes with dot syntax or similar.

mousedown
  • 155
  • 1
  • 2
  • 6

4 Answers4

3

I'd use the DOM Parser. It isn't as efficient as SAX, but if it's a simple XML file that's not too large, it's the easiest way to get up and moving.

Great tutorial on how to use it here: http://tutorials.jenkov.com/java-xml/dom.html

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
1

You might want to take a look at the XPath library. This is a more simple way of parsing xml. It's similar to building SQL queries and regex's.

http://www.ibm.com/developerworks/library/x-javaxpathapi.html

jluzwick
  • 2,005
  • 1
  • 15
  • 23
0

I'd go for a SAX Parser:

  • It's much more efficient in terms of memory, especially for larger files: you don't parse an entire document into objects, instead the parser performs a single uni-directional pass over the document and triggers events as it goes through.
  • It's actually surprisingly easy to implement: for instance take a look at Working with XML on Android by IBM. It's only listings 5 and 6 that are the actual implementation of their SAX parser so it's not a lot of code.
Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
0

You can try to use Konsume-XML: SAX/STAX/Pull APIs are too low-level and hard to use; DOM requires the XML to fit into memory and is still clunky to use. Konsume-XML is based on Pull and therefore it's extremely efficient, yet the API is higher-level and much easier to use.

Martin Vysny
  • 3,088
  • 28
  • 39