XML file creation in Perl

后端 未结 2 1705
说谎
说谎 2021-01-22 01:22

My input file is

TBLA      COLA      A    B    
TBLA      COLB      D    E    
TBLB      COLX      M    N     
TBLB      COLD      A    B   
TBLC      COLD          


        
相关标签:
2条回答
  • 2021-01-22 01:32

    Given the very simple data structure, It seems a bit unneccessary to use a whole XML writer. However, I'll assume that that the table and column names are valid XML tag names.

    Here is a simple script that reads through the data without storing it in an intermediary data structure. It works with perl5 v10 and better.

    use strict; use warnings; use feature 'say';
    
    my $last_table;
    say '<Data>';
    while(<>) {
      chomp;
      my ($table, $col, $old, $new) = split /\t/;
      s/&/&amp;/g, s/</&lt;/g for $old, $new;
      # I'll assume $table and $col have sane names
      if (not defined $last_table) {
        say "  <$table>";
      } elsif ($last_table ne $table) {
        say "  </$last_table>";
        say "  <$table>";
      }
      $last_table = $table;
      say "    <$col>";
      say "      <oldvalue>$old</oldvalue>";
      say "      <newvalue>$new</newvalue>";
      say "    </$col>";
    }
    say "  </$last_table> if defined $last_table;
    say '</Data>';
    
    0 讨论(0)
  • 2021-01-22 01:39

    Recommend is to use XML::Simple instead of writing an selfmade XML Parser. You just need to set:

    use XML::Simple;
    my $xml = XMLout($hashref, RootName => 'Data');
    
    0 讨论(0)
提交回复
热议问题