I l @ ve RuBoard |
There are two basic approaches to parsing an XML document:
This chapter focuses on the first approach. The second approach is dealt with in detail in Chapter 3, "PHP and the Document Object Model (DOM)." In order to better understand the event-driven approach, consider Listing 2.1. Listing 2.1 Simple XML Document ( fox.xml )<?xml version="1.0"?> <sentence>The <animal color="blue">fox</animal> leaped over the <vegetable color="green">cabbage</vegetable> patch and vanished into the darkness.</sentence> Now, if a SAX parser processed this document, it would generate an event trail that would look something like this: [tag opened] sentence [cdata] The [tag opened] animal [attribute name] color [attribute value] blue [cdata] fox [tag closed] animal [cdata] leaped over the [tag opened] vegetable [attribute name] color [attribute value] green [cdata] cabbage [tag closed] vegetable [cdata] patch and vanished into the darkness. [tag closed] sentence Under the SAX approach, the parser has a very clear and defined role. Its function is merely to parse the XML data that's being fed to it and to call appropriate functions (also referred to as "handlers" or "callback functions") at the application layer to handle the different types of constructs it encounters. It's up to the developer to write the code necessary to handle each event, depending on the application requirements; the SAX parser itself is completely insulated from the internals of each handler function. |
I l @ ve RuBoard |