Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8311911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:58:16+00:00 2026-06-08T19:58:16+00:00

I have a Perl script that reads a simple .csv file like below- header1,header2,header3,header4

  • 0

I have a Perl script that reads a simple .csv file like below-

"header1","header2","header3","header4"
 "12","12-JUL-2012","Active","Processed"
 "13","11-JUL-2012","In Process","Pending"
 "32","10-JUL-2012","Active","Processed"
 "24","08-JUL-2012","Active","Processed"
 .....

The aim is to convert this .csv to an .xml file something like below-

<ORDERS>
  <LIST_G_ROWS>
     <G_ROWS>
         <header1>12</header1>
         <header2>12-JUL-2012</header2>
         <header3>Active</header3>
         <header4>Processed</header4>
     </G_ROWS>
     <G_ROWS>
         <header1>13</header1>
         <header2>11-JUL-2012</header2>
         <header3>In Process</header3>
         <header4>Pending</header4>
     </G_ROWS>
....
....
   </LIST_G_ROWS>
</ORDERS>

I know that there is XML::CSV available in CPAN which will make my life easier but I want to make use of already installed XML::LibXML to create the XML, instead of installing XML::CSV. I was able to read the CSV and create the XML file as above without any issues, but I am getting a random order of the elements in the XML i.e. something like below. I need to have the order of the elements (child nodes) to be in sync with the .csv file as shown above, but I am not quite sure how do go around that. I am using a hash and sort() ing the hash didn’t quite solve the problem either.

<ORDERS>
  <LIST_G_ROWS>
     <G_ROWS>
         <header3>Active</header3>
         <header1>12</header1>
         <header4>Processed</header4>
         <header2>12-JUL-2012</header2>                          
     </G_ROWS>
 ......

and so on. Below is the snippet from my perl code

use XML::LibXML;
use strict;

my $outcsv="/path/to/data.csv";
my $$xmlFile="/path/to/data.xml";
my $headers = 0;
my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
my $root = $doc->createElement("ORDERS");
my $list = $doc->createElement("LIST_G_ROWS");
$root->appendChild($list);

open(IN,"$outcsv") || die "can not open $outcsv:  $!\n";
while(<IN>){    
    chomp($_);
    if ($headers == 0)
    {
        $_ =~ s/^\"//g;     #remove starting (")
        $_ =~ s/\"$//g;     #remove trailing (")
        @keys = split(/\",\"/,$_);  #split per ","
        s{^\s+|\s+$}{}g foreach @keys;  #remove leading and trailing spaces from each field
        $headers = 1;       
    }
    else{   
        $_ =~ s/^\"//g;     #remove starting (")
        $_ =~ s/\"$//g;     #remove trailing (")    
        @vals = split(/\",\"/,$_);  #split per ","
        s{^\s+|\s+$}{}g foreach @vals;  #remove leading and trailing spaces from each field

        my %tags = map {$keys[$_] => $vals[$_]} (0..@keys-1);                   
        my $row  = $doc->createElement("G_ROWS");
        $list->appendChild($row);
        for my $name (keys %tags) {
            my $tag = $doc->createElement($name);
            my $value = $tags{$name};
            $tag->appendTextNode($value);               
            $row->appendChild($tag);
        }
    }
}
close(IN);

$doc->setDocumentElement($root);
open(OUT,">$xmlFile") || die "can not open $xmlFile:  $!\n";
print OUT $doc->toString();
close(OUT);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:58:19+00:00Added an answer on June 8, 2026 at 7:58 pm

    You could forget the %tags hash entirely. Instead, loop over the indices of @keys:

    for my $i (0 .. @keys - 1) {
        my $key   = $keys[$i];
        my $value = $values[$i];
        my $tag   = $doc->createElement($key);
        $tag->appendTextNode($value);
        $row->appendChild($tag);
    }
    

    That way, the ordering of your keys is preserved. When a hash is used, the ordering is indeterminate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a perl script that's reading an INI file like this: [placeholder_title] Hostname
I am trying to write a simple Perl script that reads a *.csv, places
I have a Perl script that reads a command file and restarts itself if
I have a Perl script that reads a XML file that doesn't have content,
I have a Perl script that uses WWW::Mechanize to read from a file and
On Computer A (my computer), I have a Perl script that accesses a file
I currently have a Perl CGI script that parses incoming XML requests using XML::Simple
I have written a Perl script that reads some data and generates an OpenOffice
I am writing Perl script. In that I have to read log4j.xml file from
I have a simple .csv file that has that I want to extract data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.