NAME

Class::DBI::ToSax - turn database objects to SAX events

SYNOPSIS

package My::DBI;
# NB!  Must come first in inheritance chain!
use base qw( Class::DBI::ToSax Class::DBI );

# In the application...
use XML::SAX::Writer;
my $obj = My::DBI->retrieve( $x );
my $w = XML::SAX::Writer->new;
$w->start_document;
$obj->to_sax( $w );
$w->end_document;

DESCRIPTION

This module adds an extra method to Class::DBI, to_sax(). This method accepts an XML::SAX handler, and will use it to output a representation of the object in XML form, by calling methods on the handler. All the usual sorts of SAX handler can be passed in. The example above shows a writer to send the XML to stdout.

NB: This class must come first in the inheritance chain because it overrides ordinary Class::DBI methods.

The generated XML will have:

  • One wrapper element, which is the name of the table, with an id attribute.

  • One containing element for each column which has a scalar value.

  • One element for each has_a() relationship, which will be nested.

  • Zero or more elements for each has_many() relationship, each of which will be nested.

SEE ALSO

Class::DBI, XML::SAX, XML::SAX::Writer.

If you want to generate XML directly from the database without using Class::DBI, look at XML::Generator::DBI.

BUGS

start_document() and end_document() should be called by us, not the user.

We should be able to flag some fields as containing CDATA. I'm not sure of the best interface to do this, however.

AUTHOR

Dominic Mitchell, <cpan@semantico.com<gt>

COPYRIGHT AND LICENSE

Copyright 2003 by semantico

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.