270 CHAPTER 8 SIMPLE API FOR XML
270 CHAPTER 8 SIMPLE API FOR XML (SAX) Event-Based/Push Parser So, what is an event-based, or push, parser? Well, I m glad you asked that question. An event- based parser interacts with an application when specific events occur during the parsing of the XML document. Such an event may be the start or the end of an element or may be an encounter with a PI within the document. When an event occurs, the parser notifies the application and provides any pertinent information. In other words, the parser pushes the information to the application. The application is not requesting the data when it needs it, but rather it initially registers functions with the parser for the different events it would like notification for, which are then executed upon notification. Think of it in terms of a mailing list to which you can subscribe. All you need to do is register with the mailing list, and from then on, every time a new message is received from the list, the message is automatically sent to you. You do not need to keep checking the mailing list to see whether it contains any new messages. SAX in PHP The xml extension, which is the SAX handler in PHP, has been the primary XML handler since PHP 3. It has been the most stable extension and thus is widely used when dealing with XML. The expat library, http://expat.sourceforge.net/, initially served as the underlying parser for this extension. With the advent of PHP 5 and its use of the libxml2 library, a compatibility layer was written and made the default option. This means that by default, libxml2 now serves as the XML parsing library for the xml extension in PHP 5 and later, though the extension can also be built with the depreciated expat library. Enabled by default, it can be disabled in the PHP build through the –disable-xml configuration switch. (But then again, if you wanted to do this, you probably would not be reading this chapter!) You may have reasons for building this with the expat library, such as compatibility problems with your code or application. I will address some of these issues in the section Migrating from PHP 4 to PHP 5. If this is the case, you can use the configure switch –with-libexpat-dir=DIR with expat rather than libxml2. This is depreciated and should be used only in such cases where things may be broken and cannot be resolved using the libxml2 library. One other change for this extension from PHP 4 to PHP 5 is the default encoding. Originally, the default encoding used for output from this extension was ISO-8859-1. With the change to libxml2, the default encoding has changed in PHP 5.0.2 and later to UTF-8. This is true no matter which library you use to build the extension. If any existing code being upgraded to PHP 5 happens to require IISO-8859-1 as the default encoding, this is quickly and easily resolved, as you will see in the next section. Other than the potential migration issues, this chapter exclusively deals with the xml extension built using libxml2. Using the xml Extension Working with the xml extension is easy and straightforward. Once you have set up the parser and parsing begins, all your code is automatically executed. You do not need to do anything until the parsing has finished. The steps to use this extension are as follows:
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services