Java Built-in data analyzer for JSON or XML, otherwise

I want to read the data stored in a file. I have not yet decided which format to save it, but I'm looking for a format that is easy to parse. I initially thought I would go with JSON, but it looks like Java does not have a built-in JSON parser.

The saved data will be a set of records, each record consists of a set of fields. Therefore, it is not easy enough to store in a text file that can be read line by line. That is why I think I need something like JSON. But I do not want to add external libraries just for parsing the format. Any suggestions? I am new to Java.

+8
java json xml
source share
8 answers

Although many Java do not have a standard JSON parsing library, there are several libraries available that are fast, reliable, and easy to use. Many also allow you to use standard object binding methods, such as JAXB , to define your deserialization mappings using annotations.

I prefer Jackson myself. Google-GSon is also popular, and you can see how some people compare the two in this question .

You might want to be less afraid of using external libraries. It is almost always better to use an existing library that has the necessary functions, rather than writing it yourself. And with tools like Maven or Ivy to automatically calculate and load dependencies from your project definition, there really is no reason to be afraid of using libraries.

If you say that with the current state of Java XML support, you should find XML equally accessible. This answer gives a simple example of using javax.xml.parsers.DocumentBuilder to create a DOM.

+16
source share

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) { // Also store the link, or something equivalently fancy } } 

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.

+10
source share

I use GSON: http://code.google.com/p/google-gson/ to parse JSON, It is very easy to use:

 Gson gson = new Gson(); String xyzAsString = gson.toJson(xyz); 

to deserialize using JSON:

 Gson gson = new Gson(); Classname xyz = gson.fromJson(JSONedString, Classname.class); 

for more detailed examples see here: https://sites.google.com/site/gson/gson-user-guide

+6
source share

You have already accepted, but everyone does not seem to know that Java has a standard JSON library. Since JDK 7 there is javax.json in the standard lib library.

+5
source share

Java provides SAXParser for XML parsing.

+1
source share

If you are programming netbeans, you can use dtd to generate an xml scanner. Just right-click on the dtd file and select "Generate DOM scanner"

+1
source share

javax.json is a Java package - note also that there is a very lightweight Java alternative to SAX called StAX (Streaming API for XML) .

JSON v XML in the application that you offer, in my opinion, depends a lot on what you are going to do with the data and how you are going to process it. For example, if you send data to a web page and must use object notation to process it using JavaScript, then JSON is the obvious choice. If you just want to display it, you might want to consider XHTML, and let your backend choose what is displayed. If you transfer data between different industrial computers in B2B applications, you probably need to use XML and tags defined by industry standards.

0
source share

JSON is excellent, better than XML.

Why don't you want to add external libraries? If you really can’t use, you can rewrite the parser. Just implementing a parser is not difficult.

-one
source share

All Articles