CSV to XML works o.k - having prob taking a field from the CSV to name the XML file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hutch75
    New Member
    • Feb 2008
    • 12

    CSV to XML works o.k - having prob taking a field from the CSV to name the XML file

    Hi folks,

    I've been dead-ending on this issue, and I'm dusting off my brain to re-engage with Perl, so pls bear with me.

    Here's an example of what I'm trying to accomplish, have tried several variants to resolve; but am getting error mssgs to the effect of: Global symbol "@site" requires explicit package name at csvconvert-BETA.pl line 16.

    Overview is I'm uploading a .CSV file to a tmp directory on a solaris box, and then running the script to read the data and convert to an XML format. I've had success with that portion, so now I'm trying to find a way to read line 4, column 4 (for example) and apply that to name the XML file. (previously I had statically assigned the directory and name and that worked fine, but want the script to be able to dynamically base it on a value in the .CSV file as there will need to be many uniquely named XML files in the same directory)

    [CODE=perl]

    use strict;

    # Open the exchange-script.csv file for input
    open(CSV_FILE, "/tmp/exchange-script.csv") ||
    die "Can't open file: $!";

    #set variable
    my $count=0;

    while(<CSV_FILE >) {

    #only want to take data from line 4
    if ($count==4) {

    # Split each field, on the comma delimiter, into an array
    my @fields = split(/,/, $_);
    }
    # Open a new xml file for output and name according to column 4 on line 4
    open(XML_FILE, ">/exdir/dir2/ems/$fields[3].xml") ||
    die "Can't open file: $!";
    }
    # Print the initial XML header and the root element
    print XML_FILE "<?xml version=\"1.0\" ?>\n";
    print XML_FILE "<CEV>\n";
    print XML_FILE " <Management_Ite m_File>/somewhere.xml</Management_Item _File>\n";
    print XML_FILE " <Icon_Groups>\n ";
    print XML_FILE " <Icon_Group>\n" ;
    print XML_FILE " <Name>Digital_I P_1_27</Name>\n";
    print XML_FILE " <Symbol_Type>Ca rds:Card</Symbol_Type>\n" ;
    print XML_FILE "\n";
    print XML_FILE " <Background_Ima ge>/something.gif</Background_Imag e>\n";
    print XML_FILE "\n";

    # The while loop to traverse through each line in exchange-script.csv
    while(<CSV_FILE >) {
    chomp; # Delete the new line char for each line

    # Split each field, on the comma delimiter, into an array
    my @fields = split(/,/, $_);

    print XML_FILE<<"EOF" ;
    <icon>
    <Alarm_Status_I D>$fields[8]</Alarm_Status_ID >
    <Label>$field s[2]</Label>
    <X_Coordinate>$ fields[7]</X_Coordinate>
    <Y_Coordinate>$ fields[0]</Y_Coordinate>
    <Width>$field s[6]</Width>
    <Height>$fiel ds[7]</Height>
    <Symbol_Type>En vMSIcon:Generic </Symbol_Type>
    <Optional>$fiel ds[9]</Optional>
    </icon>
    EOF
    }

    # Close the root element
    print XML_FILE " </Icon_Group>\n";
    print XML_FILE " </Icon_Groups>\n" ;
    print XML_FILE "</CEV>";

    # Close all open files
    close CSV_FILE;
    close XML_FILE

    [/CODE]


    Thanks for any assist!

    Cheers, Hutch
    Last edited by eWish; Feb 20 '08, 02:14 PM. Reason: Fixed Code Tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    It's not clear what you want to do with lines 1, 2 and 3, but if you just want to skip them (untested code):

    [CODE=perl]use strict;
    use warnings;

    # Open the exchange-script.csv file for input
    open(CSV_FILE, "/tmp/exchange-script.csv") or
    die "Can't open file: $!";

    while (<CSV_FILE>) {
    next if $. < 4;
    # only want to take data from line 4
    # $. stores the value of the current line
    # being read from the file
    if ($. == 4) {
    # Split each field, on the comma delimiter, into an array
    my $filename = (split(/,/, $_))[3];

    # Open a new xml file for output and name according to column 4 on line 4
    open(XML_FILE, ">/exdir/dir2/ems/$filename.xml") or
    die "Can't open file: $!";

    # Print the initial XML header and the root element
    print XML_FILE qq{<?xml version=\"1.0\" ?>
    <CEV>
    <Management_Ite m_File>/somewhere.xml</Management_Item _File>
    <Icon_Groups>
    <Icon_Group>
    <Name>Digital_I P_1_27</Name>
    <Symbol_Type>Ca rds:Card</Symbol_Type>

    <Background_Ima ge>/something.gif</Background_Imag e>
    };
    next;
    }

    chomp; # Delete the new line char for each line

    # Split each field, on the comma delimiter, into an array
    my @fields = split(/,/);

    print XML_FILE<<"EOF" ;
    <icon>
    <Alarm_Status_I D>$fields[8]</Alarm_Status_ID >
    <Label>$field s[2]</Label>
    <X_Coordinate>$ fields[7]</X_Coordinate>
    <Y_Coordinate>$ fields[0]</Y_Coordinate>
    <Width>$field s[6]</Width>
    <Height>$fiel ds[7]</Height>
    <Symbol_Type>En vMSIcon:Generic </Symbol_Type>
    <Optional>$fiel ds[9]</Optional>
    </icon>
    EOF

    # Close the root element
    print XML_FILE " </Icon_Group>\n";
    print XML_FILE " </Icon_Groups>\n" ;
    print XML_FILE "</CEV>";

    close XML_FILE;
    }
    close CSV_FILE; [/CODE]

    Comment

    • hutch75
      New Member
      • Feb 2008
      • 12

      #3
      Many thanks, the input twas not ignored despite my latency in responding.

      Helped a great deal... I will in turn follow up with posting the complete script when complete. Which, i trust, may help others along the way!

      The present enhancement I'm trying to integrate is a means to ping (query) a remote host before SNMPSET commands are issued to it. Have already integrated a mechanism to time-out based on number of attempts -- yet I would like to use something that pings the remote network element to verify the IP address passed along through the CLI ARGV is both reachable and valid before it goes into a cycle of attempts.

      Trying to shy away from using any external modules if possible. which i'm supposing net::ping is one of them?

      granted my understanding, as previously stated, is limited

      Promise to post script(s) -- yes, there are a total of 3 + 1 to sequentially execute them ....when complete..

      and i will do so in a mostly cogent manner :-)

      Cheers,

      hutch

      Comment

      Working...