1. XML::Writer::Simple
Alberto Sim˜es
o
ambs@cpan.org
Portuguese Perl Workshop 2008
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple
2. Why?
XML wasn’t designed to be written manually;
So, we normally generate XML;
But then, we need to write it (in programs)
And we do not want to do that!
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple
4. Possible Perl code
print quot; <agenda>nquot;;
for $contact (@contacts) {
print quot;<contact>nquot;;
print quot; <name>$contact->{name}</name>nquot;;
print quot; <email>$contact->{mail}</email>nquot;;
print quot; <tel>$contact->{tel}</tel>nquot;;
print quot;</contact>nquot;;
}
print quot; </agenda>nquot;;
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple
5. Using XML::Writer::Simple
print agenda(
map { contact(
name($_->{name}),
email($_->{mail}),
tel($_->{tel})) } @contacts )
Yeah, stolen from CGI!
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple
6. What tags to use?
we want a function for each tag:
easy for HTML!
and for XML?
what are the valid tags?
who tell us?
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple
7. Full XML::Writer::Simple example
use XML::Writer::Simple
tags => [qw/agenda contact name email tel/];
print agenda(
map { contact(
name($_->{name}),
email($_->{mail}),
tel($_->{tel})) } @contacts )
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple
8. Full XML::Writer::Simple example using a DTD
use XML::Writer::Simple
dtd => quot;agenda.dtdquot;;
print agenda(
map { contact(
name($_->{name}),
email($_->{mail}),
tel($_->{tel})) } @contacts )
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple
9. Full XML::Writer::Simple example using a sample file
use XML::Writer::Simple
xml => [qw/mycontacts.xml morecontacts.xml/];
print agenda(
map { contact(
name($_->{name}),
email($_->{mail}),
tel($_->{tel})) } @contacts )
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple
10. And about attributes?
they do not need to be declared!
just use them as you would under CGI.
that is, use as first parameter an hash reference.
use XML::Writer::Simple
xml => [qw/mycontacts.xml morecontacts.xml/];
print agenda(
map { contact( { owner => quot;ambs@di.uminho.ptquot; },
name($_->{name}),
email($_->{mail}),
tel($_->{tel})) } @contacts )
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple
11. And more! PowerTags
PowerTags:
apply more than one XML tag;
follow the Perl data structure;
PowerTag by example:
ul_li(quot;aquot;,quot;bquot;,quot;cquot;)
<ul><li>a</li><li>b</li><li>c</li></ul>
ul_li({attr=>quot;valquot;},quot;aquot;,quot;bquot;)
<ul attr=quot;valquot;><li>a</li><li>b</li></ul>
or yet...
table_tr_td( [quot;aquot;,quot;bquot;,quot;cquot;],[quot;dquot;,quot;equot;,quot;fquot;] );
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple
12. PowerTags — How to define them?
n levels;
Can be declared...
use XML::Writer::Simple
powertags=>[qw/ul_li ol_li/];
Can be created in runtime...
powertag(quot;tablequot;,quot;trquot;,quot;tdquot;);
Alberto Sim˜es ambs@cpan.org
o XML::Writer::Simple