Another similar module is XML::Toolkit by perigrin. This compiles a single XML file to a series of Moose classes. I experimented with this module early on. Personally I think a series of methods in a single class might be more appropriate for programmatic control of a single XML file. perigrin somewhat agrees with me because we have both compared DBIx::Class, which is object-based, with all other ORMS which are limited by being class-based. (The ORM-talk is relevant because me and perigrin both agree that XML::Toolkit is the DBIx::Class::Loader of XML). Also, I found it to be quite verbose for even a simple XML example. Perhaps a compiler-compiler could have allowed for simpler usage. For instance to generate this XML:
you need this XML::Toolkit:<note> <to>Bob</to> <from>Alice</from> <heading>Secret</heading> <body>Shhh!</body> </note>
but only this much XML::Element::Tololmy $document = MyApp::Note->new( to_collection => [MyApp::To->new(text => 'Bob')], from_collection => [MyApp::From->new(text => 'Alice')], headings => [MyApp::Heading->new(text => 'Secret' )], body_collection => [MyApp::Body->new(text=>'Shh!')], )
In other words, one data definition, one constructor call and one method class versus no data definition, 5 constructor calls.my %data = ( to => 'Bob', from => 'Alice', heading => 'Secret', Body => 'Shhh!' ); MyApp::Note->new(data => \%data)->tree->as_XML;
my %data = ( george => [ { age => 45} , 'some content' ] );
I think I like the former approach better.my %data = ( george => 'some content' ] ); my %attr = ( george => {age 45 });
# call the superclass to render the simple data in the hashref my $lol = do { local $self->{data} = \%tmpdata; super(); }; # now rewrite part of the loltree with repetition my @newlol; for my $invoice_line ( @{$array_rows_for_single_invoice} ) { my $aref = [ InvoiceLineAdd => [ ItemRef => [ ListID => $invoice_line->{product_listid} + ] ], [ Amount => $invoice_line->{amount} ], ]; push @newlol, $aref; } my ($dump) = rmap_array { if ( $_->[0] eq 'InvoiceAdd' ) { use List::MoreUtils qw(first_index); my $i = first_index { ref $_ and $_->[0] eq 'SetCredit' } +@$_; splice @$_, ++$i, 1, @newlol; # No need to drill down any further cut($_); } else { $_; } } $lol;
-- Terence Parr, "Enforcing Strict Model View Separation in Template Engines"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: generating XML with data structures and Perl classes - the XML::Element::Tolol approach
by Anonymous Monk on Jul 11, 2011 at 16:19 UTC | |
by perigrin (Sexton) on Jul 11, 2011 at 16:20 UTC |