Comparison of several common XML parsers in Java

xiaoxiao2021-03-06  45

The commonly used XML parsers are: SAX, DOM, XERCES

1. The advantage of SAX processing is very similar to the advantages of streaming. The analysis can start immediately, not waiting for all the data being processed. Moreover, since the application is only checked when data is read, it is not necessary to store the data in memory. This is a huge advantage for large documents. In fact, the application does not even have to resolve the entire document; it can stop parsing when a condition is satisfied. In general, SAX is still much faster than its alternative DOM. On the other hand, since the application does not store data in any way, it is impossible to change the data using SAX or in the data stream.

2, DOM, and generalized tree-based processing have several advantages. First, since the tree is lasting in memory, it can modify it so that the application can make changes to the data and structure. It can also navigate up and down at any time, rather than being a one-time processing like SAX. DOM should be much simpler. On the other hand, constructing such a tree is constructed in memory. The case of large files completely occupied system memory capacity is not seen. In addition, creating a DOM tree may be a slow process.

3, select DOM or select SAX, depending on the following factor: the purpose of the application: If you plan to make changes to the data and output it to XML, then in most cases, DOM is appropriate. It is not to say that the data cannot be changed using SAX, but the process should be much more complicated because you must make a copy of the data instead of making changes to the data itself. Data capacity: For large files, SAX is a better choice. How to use: If only a small part of the data is used, use SAX to extract the part of the data to the application. On the other hand, if you know that you will return back to a lot of information, SAX may not choose the right choice. The need for speed: SAX implementation is usually faster than the DOM. SAX and DOM are not mutually exclusive, remember this is important. You can use the DOM to create a SAX event stream, or you can use SAX to create a DOM tree. In fact, most parsers used to create a DOM tree actually use SAX to complete this task!

4, SAX, DOM is two ways to analyze XML documents (there is no specific implementation, only interfaces), so it is not an interpreter. If there is them, you can't complete the processing of the XML document. SAX pack is org.xml.sax, the DOM package is org.w3c.dom, the name of the package is important, it helps you understand the relationship between them.

5. JAXP is API, and he encapsulates two interfaces of SAX / DOM. And on the basis of SAX / DOM, a relatively simple API is made for developers. JAXP's package is javax.xml.parsers, you can look at the JAXP source file, which contains a reference to SAX or DOM (Import) JAXP is not a specific implementation, he is just a set of APIs. If you just have JAXP that is unable to work, (in fact, JAXP only completed the package of SAX, DOM, generated DocumentBuilderFactory / DocumentBuilder and SaxParserFactory SaxParser. It is factory mode in design mode, his benefits are specific objects (explanation) Establish a subclass of completion)

转载请注明原文地址:https://www.9cbs.com/read-58540.html

New Post(0)