In order to tell the SAX to check the document for validity a correspodning property of SAX-parser factory should be set to true, like follows: // create new factory is ordinary way: SAXParser Factory factory = SAXParser Instance(); // specifies that the parser produced by this factory will // validate documents as they are parsed.
Many tasks, such as indexing, conversion to other formats, very simple formatting, and the like, can be done that way.
A SAX parser only needs to report each parsing event as it happens, and normally discards almost all of that information once reported (it does, however, keep some things, for example a list of all elements that have not been closed yet, in order to catch later errors such as end-tags in the wrong order).
Thus, the minimum memory required for a SAX parser is proportional to the maximum depth of the XML file (i.e., of the XML tree) and the maximum data involved in a single XML event (such as the name and attributes of a single start-tag, or the content of a processing instruction, etc.). A DOM parser, in contrast, has to build a tree representation of the entire document in memory to begin with, thus using memory that increases with the entire document length.
Other tasks, such as sorting, rearranging sections, getting from a link to its target, looking up information on one element to help process a later one, and the like, require accessing the document structure in complex orders and will be much faster with DOM than with multiple SAX passes.
Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.