WebReference.com - Part 4 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (4/5) | WebReference

WebReference.com - Part 4 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (4/5)

To page 1To page 2To page 3current pageTo page 5
[previous] [next]

Professional PHP4 XML, Chapter 10: Putting It Together

Using SAX To Write XML Data

SAX can be used to generate XML documents. This is a use of SAX that is not seen very often and can lead to some good ideas once understood.

The idea is to write a SAX parser for non-XML data. We will use the examples of SAX filters we've already seen, write a new parser, and then apply all the filters we want to the new parser.

This is a parser that extends the AbstractSAXParser class, but parses a to-do plaintext file using the parsing class we used before:

class ToDoTxtParser extends AbstractSAXParser 
{
    var $persons = Array();
    function ToDoTxtParser($txt) 
    {
        $parser = new ParseToDoTxt();
        $parser->ParseFile($txt);
        $this->persons = $parser->GetPersons();
    }
    function Parse() 
    {
        $this->StartElementHandler($this, "to-do", Array());
        foreach ($this->persons as $person => $tasks) {
            $this->StartElementHandler($this, "person", Array());
            $this->StartElementHandler($this, "name", Array());
            $this->CharacterDataHandler($this, XmlEntities($person));
            $this->EndElementHandler($this, "name");
            $this->StartElementHandler($this, "tasks", Array());
            foreach ($tasks as $task) {
                $this->StartElementHandler($this, "task", Array());
                $this->CharacterDataHandler($this, XmlEntities($task));
                $this->EndElementHandler($this, "task");
            }
            $this->EndElementHandler($this, "tasks");
            $this->EndElementHandler($this, "person");
        }
        $this->EndElementHandler($this, "to-do");
    }
}

As we can see, we parse the plaintext file and then traverse the array generating events for the XML elements that we want to produce. As we did with the manual writing approach, special characters must be converted to XML entities.

It would have been more efficient to make this class parse the plaintext file, but since we had little space we decided to reuse the parsing class we had. If we are parsing large non-XML files we should try to parse the file on-the-fly, that is, generate the proper SAX events as we read the non-XML file.

This is the PHP code used to generate an XML representation of the to-do plaintext file:

$f1 = new ToDoTxtParser("todo.txt");
$f2 = new FilterOutput();
$f1->SetListener($f2);
$f1->Parse();

Here are some of the advantages and disadvantages of using SAX:

AdvantagesDisadvantages
Can be used as the start of a filter chain for many processing tasksThe parser is responsible for generating well-formed XML data converting special characters to entities
Allows streaming - we can generate the XML file as we read the non-XML data 

To page 1To page 2To page 3current pageTo page 5
[previous] [next]

Created: September 3, 2002
Revised: September 3, 2002

URL: https://webreference.com/programming/php/php4xml/chap10/4/4.html