Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Building XML with tab delimited

by choroba (Cardinal)
on Aug 11, 2016 at 00:21 UTC ( [id://1169541]=note: print w/replies, xml ) Need Help??


in reply to Building XML with tab delimited

XML::LibXML is great for XML processing, but not so great for producing it. Some people prefer XML::Writer for that, but for a fixed structure like here, you can also use Template:
#!/usr/bin/perl use warnings; use strict; use Template; my %data; <> for 1, 2; # Skip header. while (<>) { chomp; my ($r1, $r2, $r3, $pin, $name, $zip, $date, $time) = split /\t/; $data{$pin}{reasons} = [ $r1, $r2, $r3 ]; @{ $data{$pin} }{qw{ name zip }} = ($name, $zip); push @{ $data{$pin}{charge} }, [ $date, $time ]; } my $template = 'Template'->new; $template->process(*DATA{IO}, { data => \%data }); __DATA__ <XML_FILE> [% FOR pin IN data.keys %] <REPORT TYPE="AB"> [%- i = 1 ; FOR r IN data.$pin.reasons %] <REASON[% i %]>[% r %]</REASON[% i %]>[% i = i + 1; END %] <PERSON> <PIN>[% pin %]</PIN> <NAME>[% data.$pin.name %]</NAME> <ZIP>[% data.$pin.zip %]</ZIP> </PERSON> [% FOR charge IN data.$pin.charge %] <CHARGE> <DATE>[% charge.0 %]</DATE> <TIME>[% charge.1 %]</TIME> </CHARGE> [% END %] </REPORT> [% END %]</XML_FILE>
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Building XML with tab delimited
by sannag (Sexton) on Aug 11, 2016 at 02:49 UTC
    Thank you! to be honest I hit a mental block using XML:LibXML. I already invested quit a bit of time building the program in XML::LibXML if I fail to find a solution with in LibXML I will switch to XML:Writer. :(
Re^2: Building XML with tab delimited
by Cow1337killr (Monk) on Aug 13, 2016 at 04:04 UTC

    Thank you, choroba, for your solution.

    (I was able to run it on my Strawberry Perl installation.)

    I learned about the Template module, the front-end module to the Template Toolkit.

    I also learned about *DATA{IO}, which apparently points to the data below the special literal __DATA__.

    I used Super Search and found the Using the DATA file handle for ARGV Perl Monk Meditation. This cleared up my questions about the special literal __DATA__ values IO slot, *DATA{IO}, of the *DATA typeglob.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1169541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-19 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found