There are two main methods of manipulating XML: Dom and sax. Dom will read the entire XML into memory, parse into a tree, so take up memory, parsing slow, the advantage is that you can arbitrarily traverse the tree node. Sax is a stream pattern, side-reading side parsing, memory-intensive, parsing fast, the disadvantage is that we need to handle the event ourselves.
1. The DOM is memory-based and, regardless of the size of the file, pre-loads all the content into memory. Thus consuming a lot of memory space. and Sax is event-based. When an event is triggered, a portion of the corresponding XML data is obtained, regardless of the size of the XML file, which consumes only a small amount of memory space.
2. The DOM can read XML or insert data into an XML file, while Sax can read only the XML and not insert data into the file. This is also a disadvantage of sax.
3. Another disadvantage of Sax: Dom We can specify the elements to be accessed for random access, while sax is not. Sax is traversed from the beginning of the document. And can only be traversed once. This means that we cannot access the XML file at random, and we can only traverse the XML file once from beginning to end (or, of course, truncate the traversal).
Differences in how DOM and sax read XML