473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
2 1464
KevinADC
4,059 Expert 2GB
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
hutch75
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

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

Similar topics

4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
4
by: don | last post by:
I have two existing interfaces having methods with same names. Now I have to implement both intrfaces in one class. Is there any way I could implement methods with same names in both interfaces...
0
by: | last post by:
I have an application that I'm converting from traditional ASP to asp.net, using visual basic to code with. the line it doesn't like is: "If (sData(rownumber, lcnt) = sData(rownumber - 1,...
9
suzee_q00
by: suzee_q00 | last post by:
I will admit that lots of times the obvious eludes me and this is most likely one of those times but if anyone has any ideas on what I can do to make this code work, I would greatly appreciate it....
6
by: goober | last post by:
Ladies & Gentlemen: I have built a form that client-side validates and posts data to a CRM beautifully in Internet Explorer (IE) 6. The problem comes in FireFox (FF) 1.5 when everything works...
6
by: JyotiC | last post by:
hi, i am making a GUI using Tkinter, I have a button and a checkbutton. i want the button to be enable when checkbutton is on and disble when the checkbutton is off. thanx
2
by: andrewanderson | last post by:
hi can anyone help me with this prog. cant find the prob why it cant display cout<<"This is the display of your transaction"<<endl; ifstream fobj; //declare input file stream ...
0
Savage
by: Savage | last post by:
I'm making for fun a simple program which format a input file.Input file sustain of person name,lastname and date of birth.Output file si supposed to be forammted as following: NAME ...
8
by: sabby | last post by:
I want to use the getline() so that i can enter a entire name in on line. (with spaces) The prob is that i am initializing the variable as "N/A" and saving it to a text file. it is declared as a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.