SAX (Simple API for XML) and Dom (Document Object Model) are currently two major XML APIs. Almost all commercial XML parsers simultaneously implement these two interfaces. Therefore, if your program uses sax or DOM APIs, your program is transparent to the XML parser.
1. Dom maps XML documents with a layered object model. While sax converts the elements in the document into objects for processing.
2. Dom loads the document into the memory for processing, while sax, on the contrary, can detect an upcoming XML stream, which does not require all XML code to be loaded into the memory at the same time.
How does the sax processing work?
Sax processes the XML streams while reading them, much like the previous ticker tape ). Consider the following XML code snippet:
<? XML version = "1.0"?>
<Samples>
<Server> UNIX </Server>
<Monitor> color </monitor>
</Samples>
The sax processor that analyzes this code snippet generally generates the following events:
Start document
Start Element (samples)
Characters (white space)
Start Element (server)
Characters (UNIX)
End Element (server)
Characters (white space)
Start Element (Monitor)
Characters (color)
End Element (Monitor)
Characters (white space)
End Element (samples)
The sax API allows developers to capture and respond to these events.
The following steps are involved in the processing of sax:
1. Create an event handler.
2. Create a SAX Parser.
3. Allocate event handlers to the parser.
4. parse the document and send each event to the event handler.
Advantages and disadvantages of event-based processing
The advantages of this processing are very similar to those of streaming media. The analysis can start immediately, rather than waiting for all data to be processed. In addition, because the application only checks data when reading data, it does not need to store the data in the memory. This is a huge advantage for large documents. In fact, the application does not even have to parse the entire document; it can stop parsing when a condition is met. In general, Sax is much faster than its replacement Dom.
On the other hand, since the application does not store data in any way, it is impossible to use SAX to change the data or move back in the data stream.
Dom and tree-based processing
Dom is a traditional method for processing XML data. When Dom is used, data is loaded into the memory in a tree structure.
For example, the same document used as an example in "How does the sax processing work" will be represented as a node in the Dom, And the DOM uses the parent-child relationship.
Advantages and disadvantages of tree-based processing
Dom and tree-based processing in the broad sense have several advantages. First, because the tree is persistent in the memory, you can modify it so that the application can change the data and structure. It can also navigate up and down the tree at any time, rather than one-time processing like sax. Dom is much easier to use.
On the other hand, constructing such a tree in the memory involves a lot of overhead. It is not uncommon for large files to fully occupy the system memory capacity. In addition, creating a DOM tree may be a slow process.
How to select
Whether to select Dom or sax depends on the following factors:
1. Purpose of the application: If you plan to make changes to the data and output it as XML, Dom is the right choice in most cases. It doesn't mean that you can't change data by using sax, but the process is much more complicated, because you must copy the data instead of making changes to the data itself.
2. Data capacity: for large files, Sax is a better choice.
How to use data: If only a small amount of data is used, it may be better to use SAX to extract the data to the application. On the other hand, if you know that you will reference a large amount of information that has been processed in the future, Sax may not be an appropriate choice.
3. speed requirement: the implementation of Sax is usually faster than that of Dom.
It is important to remember that sax and Dom are not mutually exclusive. You can use Dom to create a sax event stream or a DOM tree. In fact, most Resolvers used to create the DOM tree actually use SAX to complete this task!