473,651 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

12 New Member
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 1469
KevinADC
4,059 Recognized Expert Specialist
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 New Member
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
3017
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: /mypage.asp?value1=1&amp;value2=4&amp; ... which of course means nothing to asp.
4
2745
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 without getting errors from the compiler?
0
912
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, lcnt)) AND (WhatCol.Type <> 5 AND WhatCol.Type <> 6) Then " The error message is:
9
7329
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. Here's the code: while ( ( fgets ( buf, BUFLEN, fin ) ) != NULL ) /* pulls a line from "fin" up to the length of BUFLEN and stores it in "buf" */ { rmNl ( buf ); /* remove new line */ if ( !lineWithBlanks ( buf ) ) { for (i = 0; buf !=...
6
1825
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 except the validation. In FF it posts fine to my CRM but with no validation! Here are snippets of my code together after taking out as much as I can for brevity sake.
6
2439
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
2075
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 fobj.open("trans.txt"); //open file if(!fobj) //File not opened
0
1400
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 LASTNAME DATE OF BIRTH And i finished it but there is little prob: it don't output date of birth! I thinkt about the prob and can't figure out why is that happening. To output data i used function: void output(void),i have...
8
2857
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 string. look at the code: #include <iostream.h> #include <fstream> #include <cstdlib> #include <string> using namespace std; //Account data structure
0
8345
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8789
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8693
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8456
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8570
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7293
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4279
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1584
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.