As many others have noted, Java does not send the standard JSON-parsing library as part of the JDK, so if you want to use the JDK technology suite with completely neutral dependencies, you have 3 options for parsing XML:
- XPathFactory - XPath-based parsing. Reads all XML into an in-memory data structure and allows you to execute queries on it using the XPath expression language . This is probably the slowest and most intensive memory, BUT, one of the most convenient ways to query your data. You will not write a stock trading application using this, but if you only need data from a large configuration file, it is very convenient (although there are many other specific libraries for configurations that are simpler than their own).
- DocumentBuilder - DOM-based parsing. Reads all of the XML into an in-memory data structure that you can query and go through as needed. The 2nd is the slowest and quite saturated with memory, but it is necessary if you want / need an XML DOM to store in memory so that you can work on it. It is also convenient if you want to read, query, make changes and write the DOM as a modified XML file.
- SAXParser - SAX-based parsing. Almost the fastest. Browse through XML from top to bottom by invoking the wired methods in your ContentHandler implementation (provided during parsing) each time the corresponding element is hit. It is basically like a chatty person telling you everything they do, how they do it. It is up to you to implement the implemented methods to actually do something with the data that it passes to you when it finds it.
- XMLStreamReader - The fastest parsing method and uses the lowest overhead. This is the new golden XML parsing in Java. It is similar to STAX, but instead of invoking running methods every time it finds something new, it copies the XML file and notifies the changed state that calls it that it sees new content, but does nothing with the content, until you ask him, For example, he will say something like "Now I'm looking for an open tag ... now the tag is close ... now some characters ... now the comment ...", and if you do not ask him about those the elements that he clicks (get attributes, characters, etc.), he never parses or processes them from the stream, n just misses them.
NOW, all that is said, working with these APIs, especially if you are a beginner, is not the most intuitive in the world. If you used to be familiar with XML parsing in Java, everything will be fine.
If you look at a tiny third-party JAR, I'm going to point you to my Simple XML Parser Library (SJXP) . This gives you XPath simplicity with STAX parsing efficiency; honestly (I am impartial, seriously) - this is amazing.
I spent more than a year working on this, writing a really reliable Parseing Feed system, which started as a SAX-based system, then switched to STAX, and the more I worked on it, the more I realized how easily I can abstract the pain STAX with simple rules.
You can see an example of use , but you, in fact, determine the rules that must match "/ library / book / title", will analyze all your tag contents; you can parse attributes and even qualified values ββby name (yes, it also supports namespaces).
Here is an example of parsing an RSS feed:
IRule linkRule = new DefaultRule(Type.CHARACTER, "/rss/channel/item/link") { @Override public void handleParsedCharacters(XMLParser parser, String text, Object userObject) {
Then you simply pass this rule to the parser when it is created, for example:
XMLParser parser = new XMLParser(linkRule);
And everything is ready; just give the parser your XML files using the parse method and you will get callbacks every time that path is matched.
I tested, profiled and optimized the STAX library overhead to such an extent that it does not exist. The actual matching of patches is done using cached hash codes, so I donβt even perform string comparisons inside the parser.
It is very fast and runs on Android.
If you want to do JSON instead, I highly recommend using GSON. Jackson is faster, but the API is 37 times more complex than the GSON API. You will spend more time figuring out which classes you need to use in Jackson than you will with GSON.
In addition, since the last release of GSON and the rewriting of the flow analyzer, the speed gap was closed quite a bit; you can use the flow analyzer tool to get an approximate Jackson speed if that matters.
At the same time, if you need ULTIMATE speed higher and higher, and this is the # 1 priority, use Jackson.