Printing out the Nodes of an XML Document Using SAXY

The SAXY XML parser is a Simple API for XML (SAX) parser. It is modelled on the Expat parser, so if you have used Expat before, you should find it an easy transition to SAXY.

In this brief example, we will use SAXY to print out the nodes of a simple XML string:

<book><title><![CDATA[How to use SAXY]]></title><author>John Heinstein</author></book>

We begin by including the SAXY php file and creating an instance of the parser:

//create new SAXY parser
$parser = new SAXY_Parser();

As with Expat, we set the handler for start and end tag events:

$parser->xml_set_element_handler(array(&$this, "startElement"), array(&$this, "endElement"));

We do the same for a character data event:

$parser->xml_set_character_data_handler(array(&$this, "charData"));

The string is then fed to the parser instance using the parse method:

//parse RSS XML
$parser->parse("<book><title><![CDATA[How to use SAXY]]></title><author>John Heinstein</author></book>");