XML-XMLWriter
view release on metacpan or search on metacpan
XMLWriter.pm view on Meta::CPAN
my @data=(['Name', 'Adress', 'Email', 'Sex'],
['Herbert', 'BeerAvenue 45', 'herbert@names.org', 'Male'],
['Anelise', 'SchmidtStreet 21', 'foo@bar.com', 'Female'],
['XYZ', 'ZYX', 'ZY', 'XZ'],
['etc...']);
my $doc = new XML::XMLWriter(system => 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd',
public => '-//W3C//DTD XHTML 1.0 Transitional//EN');
my $html = $doc->createRoot;
$html->head->title->_pcdata('A Table');
my $body = $html->body;
$body->h1->_pcdata('Here is a table!');
my $table = $body->table({align => 'center', cellspacing => 1, cellpadding => 2, border => 1});
for(my $i=0; $i<@data; $i++) {
my $tr = $table->tr;
foreach $_ (@{$data[$i]}) {
$i==0 ? $tr->th->_pcdata($_) : $tr->td->_pcdata($_);
}
}
$body->b->_pcdata("that's it!");
$doc->print();
=head2 Example Output
<?xml version="1.0" encoding="ISO-8859-15" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><title>A Table</title></head><body><h1>Here is a table!</h1><table align="center" cellspacing="1" cellpadding="2" border="1"><tr><th>Name</th><th>Adress</th><th>Email</th><th>Sex</th></tr><tr><td>Herbert</td><td>BeerAvenue 45</td><td>her...
=head1 DESCRIPTION
XMLWriter.pm view on Meta::CPAN
(see the C<new> method).
=item
3. A string of character data which will be appended right after
the start tag. Defaults to undef, which means no data will be added.
=back
Instead of passing the third argument you can also just do a
C<$root-E<gt>_pcdata(yourdata)>, its exactly the same.
=cut
######################################################################
sub createRoot {
my ($self,$root,%args,$text) = @_;
$self->{dtd} = XML::ParseDTD->new($self->{Conf}->{system},checklm => $self->{Conf}->{checklm}) if($self->{Conf}->{system});
$self->{root} = XML::XMLWriter::Element->new(($root||$self->{Conf}->{root}),$self->{dtd},(\%args||$self->{Conf}->{rootArgs}),$text);
return $self->{root};
XMLWriter/Element.pm view on Meta::CPAN
=head2 In General
Down there are some methods which to you might seem quite useless
since they simply call some other expression which you could also call
directly. I recommend to use them though since in feature releases
they might do more and then you surley don't want to modify your
code. I just document what they call to make clear what they do so far.
=head2 Methods
=head3 new ($elementname, $ParseDTD_object, \%arguments, $pcdata,
$Element_parent_object)
Constructor. Complains by outputting a warning if the attribute list
or some attribute value is not allowed by the DTD.
=cut
######################################################################
sub new {
XMLWriter/Element.pm view on Meta::CPAN
$self->{args} = $args;
$self->{dtd} = $dtd;
$self->{child} = [];
$self->{parent} = $parent;
$self->_text($text) if($text);
return $self;
}
######################################################################
=head3 _text (@pcdata)
Same as C<_pcdata>.
=cut
######################################################################
sub _text {
my $self = shift;
return $self->_pcdata(@_);
}
######################################################################
=head3 _pcdata (@pcdata)
Adds a XML::XMLWriter::PCData object to the child list. But returns
the elements object (C<$self>). See the POD of PCData.pm for more
information on what is done with @pcdata.
=cut
######################################################################
sub _pcdata {
my ($self,@text) = @_;
push @{$self->{child}}, XML::XMLWriter::PCData->new(@text);
return $self;
}
######################################################################
=head3 _entity ($ref)
Simply calls and returns C<_cdata('&$ref;');>.
=cut
######################################################################
sub _entity {
my($self,$ref) = @_;
return $self->_cdata("&$ref;");
}
######################################################################
=head3 _comment ($comment)
Simply calls and returns C<_cdata('E<lt>!-- $comment --E<gt>');>.
=cut
######################################################################
sub _comment {
my($self,$comment) = @_;
return $self->_cdata("<!-- $comment -->");
}
######################################################################
=head3 _pi ($target, $data)
Adds an processing instruction to the child list by calling and
returning C<_cdata('E<lt>?$target $data?E<gt>');>.
=cut
######################################################################
sub _pi {
my($self,$target,$data) = @_;
return $self->_cdata("<?$target $data?>");
}
######################################################################
=head3 _cdata (@cdata)
Simply adds every element that @cdata contains to the child list.
You can also call directly C<_push_child(@cdata)>.
Returns the object it was called on.
=cut
######################################################################
sub _cdata {
my($self,@cdata) = @_;
#push @{$self->{child}}, @cdata;
$self->_push_child(@cdata);
return $self;
}
######################################################################
=head3 _add ($element, \%arguments, $pcdata)
Adds element/tag $element to the child list. It'll produce a warning
if the DTD doesn't allow that operation. Depending on whether the DTD
says that the newly created element is a empty element or not, the
object C<_add> was called on or the newly created object is returned.
=cut
######################################################################
XMLWriter/Element.pm view on Meta::CPAN
######################################################################
sub _push_child {
my $self = shift;
push @{$self->{child}}, @_;
return 1;
}
######################################################################
=head3 AUTOLOAD (\%arguments, $pcdata);
If you call an undefined method on the object this method is called
instead.
The name of the method that you tried to call will be taken as a tag
resp. element name, then C<_add> is called and returned, passing it
the name as first argument (and forwarding the other arguments).
For every tag that starts with I<_> you have to add one I<_> more
because one will be cutted of. That means that you've to call
examples/table.pl view on Meta::CPAN
#transitional a bit more tolerant, so no warnings
#my $doc = new XML::XMLWriter(system => 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd',
# public => '-//W3C//DTD XHTML 1.0 Transitional//EN');
#we just use another encoding (doesn't matter since only ascii characters are used)
#my $doc = new XML::XMLWriter(system => 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd',
# public => '-//W3C//DTD XHTML 1.0 Transitional//EN',
# encoding => 'ISO-8859-15');
my $html = $doc->createRoot;
$html->head->title->_pcdata('A Table');
my $body = $html->body;
$body->h1->_pcdata('Here is a table!');
my $table = $body->table({align => 'center', cellspacing => 1, cellpadding => 2, border => 1});
for(my $i=0; $i<@data; $i++) {
my $tr = $table->tr;
foreach $_ (@{$data[$i]}) {
$i==0 ? $tr->th->_pcdata($_) : $tr->td->_pcdata($_);
}
}
$body->b->_pcdata("that's it!");
$doc->print();
examples/website.pl view on Meta::CPAN
use strict;
use warnings;
use XML::XMLWriter;
use POSIX;
my $version = '0.1';
my @parts = ('Download', 'Documentation', 'Contact', 'Bugs');
my $doc = new XML::XMLWriter;
my $html = $doc->createRoot();
$html->head()->title()->_pcdata('XML::XMLWriter Homepage');
my $body = $html->body();
$body->h1({style => 'text-align:center;'})->_pcdata('XML::XMLWriter');
my $p = $body->p({style => "text-align:center; font-size:9pt;"});
foreach $_ (@parts) {
$p->a({'href' => '#'.$_}, $_);
$p->_pcdata(' ');
}
$body->p()
->_pcdata('XMLWriter is a Perl 5 object class, its purpose is to make writing XML documents easier, cleaner, safer and standard conform. Its easier because of the object oriented way XML documents are written with XMLWriter. Its cleaner because of ...
->br()
->br()
->_pcdata('But still: it might be a matter of taste whether one finds XMLWriter usefull or not and it probably has some bugs (i would appreciate a lot if you report them to me), many usefull features are missing, not implemented or not even thought...
->br()
->br()
->_pcdata('XMLWriter contains 3 packages: XMLWriter.pm which gives you the document object, Element.pm which provides the element/tag objects and PCData.pm which represents the parsed character data the document contains. There\'ll probably come mo...
The most interesting class is Element.pm. It provides some methods you can call on every document element, but besides those methods it uses the AUTOLOAD feature of perl to expect every not known method name to be the name of a tag that should be add...
->br()
->br()
->_pcdata('XML::ParseDTD is supported by the ')
->a({href => 'http://fhg.freesources.org'}, 'fHG')->_parent()
->_pcdata('.');
foreach $_ (@parts) {
$body->h2()->a({name => $_}, $_);
my @arg = eval('get_part_'.$_.'($doc->get_dtd())') or die @!;
#of course would be easier to pass the p() object and add directly on it (shorter!)
#but i want to demonstrate the possibilities you have
$body->p()->_push_child(@arg);
}
$body->hr()->div({style => 'font-style:italic'})->_pcdata('Last modified: ', POSIX::ctime(time()));
#$body->_pcdata('<test>¨');
$doc->print();
sub get_part_Download {
my $dtd = shift;
return(XML::XMLWriter::Element->new('a', $dtd, {href => "XML-XMLWriter-$version.tar.gz"}, "XML-XMLWriter-$version"),
XML::XMLWriter::PCData->new(' (older versions are available from '),
XML::XMLWriter::Element->new('a', $dtd, {href => 'http://backpan.cpan.org/authors/id/M/MO/MORNI/'}, 'http://backpan.cpan.org/authors/id/M/MO/MORNI/'),
XML::XMLWriter::PCData->new(')'));
}
( run in 0.244 second using v1.01-cache-2.11-cpan-ec4f86ec37b )