As many others have pointed out, Java doesn't ship a standard JSON-parsing library as part of the JDK, so if you want to use JDK-bundled tech with absolutely NO dependencies, you have 3 XML parsing choices:
- XPathFactory - XPath-based parsing. Reads the entire XML into an in-memory data structure and allows you to execute queries on it using XPath expression language. This is probably the slowest and most memory intensive, BUT, one of the most convenient ways to query your data. You wouldn't write a stock-trading app using this, but if you just need data from a big config file, it is very handy (although for configs, there are many other specific libs for that which are easier than rolling your own).
- DocumentBuilder - DOM-based parsing. Reads the entire XML into an in-memory data structure you can query and traverse as needed. 2nd slowest and fairly memory-intense, but necessary if you want/need the XML DOM to stick around in memory so you can operate on it. Also handy if you want to read, query, make changes and write the DOM back out as a modified XML file.
- SAXParser - SAX-based parsing. Almost the fastest. Parses through the XML top-to-bottom, calling stubbed methods in your ContentHandler implementation (provided at parse time) every time the appropriate element is hit. It is basically like a chatty person telling you everything they are doing AS they do it. It is up to you to implement the stubbed out methods to actually do something with the data it is passing you as it finds it.
- XMLStreamReader - Fastest parsing method and uses the lowest overhead. This is the new golden-child of XML parsing in Java. It is similar to STAX, but instead of calling stubbed methods every time it finds something new, it rips across the XML file and notifies the caller of its modified state as it sees new content but does nothing WITH the content until you ask it for it. For example, it'll say something like "Now I'm looking at an open tag... now a close tag... now some chars... now a comment..." and unless you ask it for information about those elements it is hitting (get attributes, characters, etc.) it never actually parses and processes them out of the stream, it just skips them.
NOW, all that being said, working with these APIs especially if you are new isn't the most intuitive in the world. If you've done XML parsing in Java before, you'll be fine though.
If you WILL consider a tiny 3rd party JAR though, I am going to point you at my Simple Java XML Parser (SJXP) library. It gives you the ease of XPath with the performance of STAX parsing; honestly (I am being unbiased, seriously) -- it is awesome.
I spent more than a year working on this while writing a really robust Feed-parsing system that started off as a SAX-based system, then moved to STAX and the more I worked on it the more I realized how easily I could abstract out the pain of STAX with simple rules.
You can look at the Usage example, but you essentially define rules to match like "/library/book/title" will parse all your tag contents; you can parse attributes and even name-space qualified values (yes it supports namespaces too!)
Here is an RSS feed parser example:
IRule linkRule = new DefaultRule(Type.CHARACTER, "/rss/channel/item/link") {
@Override
public void handleParsedCharacters(XMLParser parser, String text, Object userObject) {
// Also store the link, or something equivalently fancy
}
}
Then you just pass that rule to the parser when you create it, like this:
XMLParser parser = new XMLParser(linkRule);
And you are done; just give the parser your XML files via the parse method and you'll get callbacks every time that path is matched.
I have benchmarked, profiled and optimized out the overhead of the library ontop of STAX to the point that it is measurably non-existent. The actual patch-matching is done via cached hash codes so I am not even doing string comparisons inside the parser.
It's really fast and it works on Android.
If you want to do JSON instead, I'd strongly recommend using GSON. Jackson is faster, but the API is 37x more complex than the GSON API. You'll spend more time figuring out exactly which classes you need to use in Jackson than you will with GSON.
Also since the last GSON release and the rewrite of the stream parser the speed gap has been closed quite a bit; you can use the stream parser impl of theirs to get near-Jackson parsing speeds if that is critical.
That being said, if you need ULTIMATE speed above and beyond anything and that is priority #1, then use Jackson.