XML Parser Paradigms
Now that we have discussed the low-level parser models, push and pull, let's look at the more commonly discussed models that are built on top of the push and pull concepts. The two major types of parsers are event-driven and tree-based parsers.
Event-Driven XML Parsers
An event-driven parser is a model that can be closely associated with the push model. Event-driven parsers enable an application to define events for each recognized construct, and after that construct (event) is encountered, the parser calls the correct function in the application associated with that event. For example, we can define an event that is called whenever the opening tag (for example, <name>) of an element is encountered. Simple API for XML (SAX) is the most popular event-driven parser specification. It defines a standard way to access and manipulate a SAX-compliant parser. This is a very powerful model due to its speed and efficiency. We'll discuss event-driven modules in Chapter 3, "Event-Driven Parser Modules."
Tree-Based XML Parsers
When we memorize some information (for example, a friend's telephone number), it's easier to remember it than it is to look it up in the telephone book. Unless we have amnesia, our memory serves as the fastest and most reliable source for retrieving data. If you look at the tree-based or pull parser model shown in Figure 2.2, you'll notice the parser has a brain of its own and, therefore, stores the parsed information in a data structure. After the information is stored in the memory of these data structures, it can be easily accessed from the application. One of the most popular tree-based XML parser models is called Document Object Model (DOM), which is a specification that defines the behaviors and data structures of DOM-compliant XML parsers. We'll talk more about DOM and other tree-based XML parsers in Chapter 4, "Tree-Based Parser Modules."
XML Parsers Versus XML Processors
Try not to confuse XML parsers with XML processors. XML parsers are different from processors in that an XML parser is only one of the components of an XML processor. An XML processor may include several different components required to accomplish its task.
A perfect example of an XML processor is the eXtensible Stylesheet Language (XSL) Transformations (XSLT) processor. The XSLT processor is composed of many components, one of which is the actual XML parser itself. It uses the parsing component to decompose an XML document into meaningful parts so that other XSLT components can operate on the individual components. An XSLT processor converts requires two inputs: an XML document and an XSLT stylesheet. The XSLT stylesheet is an XML document that contains rules (called templates) that describe the format of the output document. The XSLT processor then applies these rules to the input XML document and generates the desired output. For example, we can start with an XML document, use an XSLT processor, and the end product could be an HTML document (for display on a web page), or even another XML document that is a subset of the original XML document. XSLT and other related topics will be discussed in Chapter 8, "XML Transformation and Filtering."