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>

In reply to Re: Pulling from a list and inserting into XML documents by haukex
in thread Pulling from a list and inserting into XML documents by gng4life

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.