By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

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

P: 12
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)

Expand|Select|Wrap|Line Numbers
  1.  
  2. use strict;
  3.  
  4. # Open the exchange-script.csv file for input
  5. open(CSV_FILE, "/tmp/exchange-script.csv") || 
  6. die "Can't open file: $!";
  7.  
  8. #set variable
  9. my $count=0;
  10.  
  11. while(<CSV_FILE>) {
  12.  
  13.     #only want to take data from line 4
  14.     if ($count==4) {
  15.  
  16.        # Split each field, on the comma delimiter, into an array
  17.         my @fields = split(/,/, $_);
  18.     }
  19. # Open a new xml file for output and name according to column 4 on line 4
  20. open(XML_FILE, ">/exdir/dir2/ems/$fields[3].xml") || 
  21. die "Can't open file: $!";
  22. }
  23. # Print the initial XML header and the root element
  24. print XML_FILE "<?xml version=\"1.0\"?>\n";
  25. print XML_FILE "<CEV>\n";
  26. print XML_FILE "  <Management_Item_File>/somewhere.xml</Management_Item_File>\n";
  27. print XML_FILE "  <Icon_Groups>\n";
  28. print XML_FILE "    <Icon_Group>\n";
  29. print XML_FILE "      <Name>Digital_IP_1_27</Name>\n";
  30. print XML_FILE "      <Symbol_Type>Cards:Card</Symbol_Type>\n";
  31. print XML_FILE "\n";
  32. print XML_FILE "        <Background_Image>/something.gif</Background_Image>\n";
  33. print XML_FILE "\n";
  34.  
  35. # The while loop to traverse through each line in exchange-script.csv
  36. while(<CSV_FILE>) {
  37. chomp; # Delete the new line char for each line
  38.  
  39. # Split each field, on the comma delimiter, into an array
  40. my @fields = split(/,/, $_);
  41.  
  42. print XML_FILE<<"EOF";
  43.       <icon>
  44.         <Alarm_Status_ID>$fields[8]</Alarm_Status_ID>
  45.         <Label>$fields[2]</Label>
  46.         <X_Coordinate>$fields[7]</X_Coordinate>
  47.         <Y_Coordinate>$fields[0]</Y_Coordinate>
  48.         <Width>$fields[6]</Width>
  49.         <Height>$fields[7]</Height>
  50.         <Symbol_Type>EnvMSIcon:Generic</Symbol_Type>
  51.         <Optional>$fields[9]</Optional>
  52.       </icon>
  53. EOF
  54. }
  55.  
  56. # Close the root element
  57. print XML_FILE "    </Icon_Group>\n";
  58. print XML_FILE "  </Icon_Groups>\n";
  59. print XML_FILE "</CEV>";
  60.  
  61. # Close all open files
  62. close CSV_FILE;
  63. close XML_FILE
  64.  
  65.  

Thanks for any assist!

Cheers, Hutch
Feb 20 '08 #1
Share this Question
Share on Google+
2 Replies


KevinADC
Expert 2.5K+
P: 4,059
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):

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3.  
  4. # Open the exchange-script.csv file for input
  5. open(CSV_FILE, "/tmp/exchange-script.csv") or
  6.    die "Can't open file: $!";
  7.  
  8. while (<CSV_FILE>) {
  9.    next if $. < 4;
  10.    # only want to take data from line 4
  11.    # $. stores the value of the current line
  12.    # being read from the file
  13.    if ($. == 4) { 
  14.       # Split each field, on the comma delimiter, into an array
  15.       my $filename = (split(/,/, $_))[3];
  16.  
  17.    # Open a new xml file for output and name according to column 4 on line 4
  18.    open(XML_FILE, ">/exdir/dir2/ems/$filename.xml") or
  19.       die "Can't open file: $!";
  20.  
  21.    # Print the initial XML header and the root element
  22.    print XML_FILE qq{<?xml version=\"1.0\"?>
  23. <CEV>
  24.  <Management_Item_File>/somewhere.xml</Management_Item_File>
  25.  <Icon_Groups>
  26.  <Icon_Group>
  27.  <Name>Digital_IP_1_27</Name>
  28.  <Symbol_Type>Cards:Card</Symbol_Type>
  29.  
  30.  <Background_Image>/something.gif</Background_Image>
  31. };
  32.    next;
  33.    }
  34.  
  35.    chomp; # Delete the new line char for each line
  36.  
  37.    # Split each field, on the comma delimiter, into an array
  38.    my @fields = split(/,/);
  39.  
  40.    print XML_FILE<<"EOF";
  41. <icon>
  42. <Alarm_Status_ID>$fields[8]</Alarm_Status_ID>
  43. <Label>$fields[2]</Label>
  44. <X_Coordinate>$fields[7]</X_Coordinate>
  45. <Y_Coordinate>$fields[0]</Y_Coordinate>
  46. <Width>$fields[6]</Width>
  47. <Height>$fields[7]</Height>
  48. <Symbol_Type>EnvMSIcon:Generic</Symbol_Type>
  49. <Optional>$fields[9]</Optional>
  50. </icon>
  51. EOF
  52.  
  53.    # Close the root element
  54.    print XML_FILE " </Icon_Group>\n";
  55.    print XML_FILE " </Icon_Groups>\n";
  56.    print XML_FILE "</CEV>";
  57.  
  58.    close XML_FILE;
  59. }
  60. close CSV_FILE;    
Feb 20 '08 #2

P: 12
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
Mar 14 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.