spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / php / php4xml / chap10 / 4 To page 1To page 2To page 3current pageTo page 5
[previous] [next]

Professional PHP4 XML, Chapter 10: Putting It Together

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


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 

home / programming / php / php4xml / chap10 / 4 To page 1To page 2To page 3current pageTo page 5
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

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

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