in reply to Pulling from a list and inserting into XML documents
Two modules you should look at are Text::CSV and XML::Twig. There are several other good XML modules, but this one is well-suited for processing XML documents as a stream. What is unclear to me is whether your XML input files are all different, because if they're all the same, that means you're basically doing some template processing, and maybe a system like Template-Toolkit might be better for this. Also, it's not clear to me how you want to match up input files to rows of CSV data. Anyway, here's a quick sample to get you started, updating this to read/write from actual files is left as an exercise for the reader ;-)
use warnings; use strict; use Text::CSV; use XML::Twig; my %data; my $twig = XML::Twig->new( twig_print_outside_roots => 1, twig_roots => { 'HOST_NAME' => sub { $_[1]->set_text($data{host})->print }, 'HOST_IP' => sub { $_[1]->set_text($data{ip} )->print }, 'HOST_MAC' => sub { $_[1]->set_text($data{mac} )->print }, } ); my $XML = <<'END_XML'; # just for demo <root> <HOST_NAME></HOST_NAME> <HOST_IP></HOST_IP> <HOST_MAC></HOST_MAC> </root> END_XML my $csv = Text::CSV->new({binary=>1,auto_diag=>2}); while ( my $row = $csv->getline(*DATA) ) { @data{"host","ip","mac"} = @$row; # here's where you'd need to match up XML file with data row $twig->parse($XML); } $csv->eof or $csv->error_diag; __DATA__ hosta,1.1.1.1,00000C123456, hostb,2.2.2.2,00000C123457, hostc,3.3.3.3,00000C123458,
Output:
<root> <HOST_NAME>hosta</HOST_NAME> <HOST_IP>1.1.1.1</HOST_IP> <HOST_MAC>00000C123456</HOST_MAC> </root> <root> <HOST_NAME>hostb</HOST_NAME> <HOST_IP>2.2.2.2</HOST_IP> <HOST_MAC>00000C123457</HOST_MAC> </root> <root> <HOST_NAME>hostc</HOST_NAME> <HOST_IP>3.3.3.3</HOST_IP> <HOST_MAC>00000C123458</HOST_MAC> </root>
|
|---|