在Perl中使用制表符分隔文件

时间:2016-08-10 18:40:57

标签: xml perl libxml2 tab-delimited

我是Perl的新手。请检查我的错误。

我有以下输入制表符分隔文件,需要构建为XML,'pin'number是唯一标识符。如果'Pin'匹配则 填充并在报告下显示多个“充电”。

Reason1 Reason2 Reason3 Pin Name    Zip Date    Time

data1   data2   data3   Pin 1   data5   data6   data7   data8
data1   data2   data3   Pin 1   data5   data6   data9   data10
data1   data2   data3   Pin 1   data5   data6   data11  data12

我希望它构建以下XML

<XML_FILE>
  <REPORT TYPE="AB">
    <REASON1>data1</REASON>
    <REASON2>data2</REASON2>
    <REASON3>data3</REASON3>
    <PERSON>
      <PIN>Pin 1</PIN>
      <NAME>data5</NAME>
      <ZIP>data6<ZIP>
    </PERSON>
    <CHARGE>
      <DATE>data7</DATE>
      <TIME>data8</TIME>
    </CHARGE>
    <CHARGE>
       <DATE>data9</DATE>
       <TIME>data10<TIME>
    </CHARGE>
    <CHARGE>
       <DATE>data11</DATE>
       <TIME>data12</TIME>
    </CHARGE>
  </REPORT>
</XML_FILE>


use strict;
use XML::LibXML;

my $READFILENAME = "SomeDir\\data.txt";
my $WRITEFILENAME = "SomeDir\\test.xml";

my $doc = XML::LibXML::Document->new('1.0');
my $root = $doc->createElement("XML_FILE");
open (FILEWRITE, ">$WRITEFILENAME");
open (READFILE, $READFILENAME);
my $copy_person_pin = "XX";
foreach (<READFILE>) {
    my $line = $_; chomp $line;
    my @data = split(/\t/,$line);
    my $reason1 = $data[0];
    my $reason2 = $data[1];
    my $reason3 = $data[2];
    my $person_pin = $data[3];
    my $name = $data[4];
    my $zip = $data[5];
    my $date = $data[6];
    my $time = $data[7];
    my $report = $doc->createElement("REPORT");
    if ($person_pin ne $copy_person_pin)
    {  
      # Build the Report tags
      # I had to put $report out of if loop so that $report is avalible in else statement
      # my $report = $doc->createElement("REPORT");
      $report->setAttribute('TYPE'=>'AB');
      my @sortedReportTag = qw ( REASON1
                REASON2
                REASON3
                ); 

      my %reportHashTags;
      @reportHashTags { @sortedReportTag } = ($reason1,
                     $reason2,
                     $reason3
                         );                                                                            
      buildXMLElements(\@sortedReportTag, \%reportHashTags, $report); 

      $root-> appendChild($report);

      # Build the element for Person Tag
      my $person = $doc->createElement("PERSON");
      my @sortedPersonTag = qw ( PIN
                NAME
                ZIP); 
      my %personHashTags;
      @personHashTags { @sortedPersonTag } = ($person_pin,
                     $name,
                     $zip
                         );
      # Build the elements for Person Tag                                        
      buildXMLElements(\@sortedPersonTag, \%personHashTags, $person);
      $report-> appendChild($person); 


      # Build the elements for Charge Tag
      my $charge = $doc->createElement("CHARGE");     
      my @sortedChargeTag = qw ( DATE
                TIME
                );
      my %chargeHashTags;
      @chargeHashTags { @sortedChargeTag } = ($date,
                     $time
                      );  
      # Build the elements for Charge Tag                                     
      buildXMLElements(\@sortedChargeTag, \%chargeHashTags, $charge);
      $report-> appendChild($charge);
      $copy_person_pin = $person_pin;
    }
    else {
      my $charge = $doc->createElement("CHARGE");     
      my @sortedChargeTag = qw ( DATE
                TIME
                );
      my %chargeHashTags;
      @chargeHashTags { @sortedChargeTag } = ($date,
                     $time
                      );  
      # Build the elements for Charge Tag                                     
      buildXMLElements(\@sortedChargeTag, \%chargeHashTags, $charge);
      $report-> appendChild($charge); 
    }
}

$doc->setDocumentElement($root);

# Write the XML to a file
print FILEWRITE ($doc->toString());
close FILEWRITE;

sub buildXMLElements() {
  my($elementTags, $hashTags, $parentElement) = @_;
  for my $name (@$elementTags) {
    my $reportTag = $doc->createElement($name);
    my $reportValue = $hashTags->{$name};
    $reportTag->appendTextNode($reportValue);
    $parentElement->appendChild($reportTag);
  }
}

我得到的输出是,基本上只有第一个'CHARGE'出现而不是其他两个。

<XML_FILE>
 <REPORT TYPE="AB">
   <REASON1>data1</REASON>
   <REASON2>data2</REASON2>
   <REASON3>data3</REASON3>
   <PERSON>
    <PIN>Pin 1</PIN>
    <NAME>data5</NAME>
    <ZIP>data6<ZIP>
   </PERSON>
   <CHARGE>
    <DATE>data7</DATE>
    <TIME>data8</TIME>
   </CHARGE>
  </REPORT>
   </XML_FILE>

@stevenl感谢回复。但它不起作用。如果我按照建议在开头声明了空的$ report,那么脚本会运行,但它仍然不会添加第二次和第三次充电。它仍然具有与上面相同的输出。

1 个答案:

答案 0 :(得分:1)

  • 问题在于else$report是新创建的 尚未添加到$root
  • 的元素
  • 你应该做什么 是重用现有的$report元素而不是创建新元素 之一。

所以我会修改你的代码如下:

use strict;
use warnings;
use XML::LibXML;

my $READFILENAME = "data.txt";
my $WRITEFILENAME = "test.xml";

my $doc = XML::LibXML::Document->new('1.0');
my $root = $doc->createElement("XML_FILE");
open my $FILEWRITE, '>', $WRITEFILENAME;
open my $READFILE, '<', $READFILENAME;
my $copy_person_pin = "XX";
my $report;
while (my $line = <$READFILE>) {
    chomp $line;
    my @data = split(/\s+/,$line);
    my $reason1 = $data[0];
    my $reason2 = $data[1];
    my $reason3 = $data[2];
    my $person_pin = $data[3];
    my $name = $data[4];
    my $zip = $data[5];
    my $date = $data[6];
    my $time = $data[7];

    if ($person_pin ne $copy_person_pin)
    {  
        $report = $doc->createElement("REPORT");
        # Build the Report tags
        # I had to put $report out of if loop so that $report is avalible in else statement
        # my $report = $doc->createElement("REPORT");
        $report->setAttribute('TYPE'=>'AB');
        my @sortedReportTag = qw ( REASON1
                  REASON2
                  REASON3
                  ); 

        my %reportHashTags;
        @reportHashTags { @sortedReportTag } = ($reason1,
                       $reason2,
                       $reason3
                           );                                                                            
        buildXMLElements(\@sortedReportTag, \%reportHashTags, $report); 

        $root-> appendChild($report);

        # Build the element for Person Tag
        my $person = $doc->createElement("PERSON");
        my @sortedPersonTag = qw ( PIN
                  NAME
                  ZIP); 
        my %personHashTags;
        @personHashTags { @sortedPersonTag } = ($person_pin,
                       $name,
                       $zip
                           );
        # Build the elements for Person Tag                                        
        buildXMLElements(\@sortedPersonTag, \%personHashTags, $person);
        $report-> appendChild($person); 


        # Build the elements for Charge Tag
        my $charge = $doc->createElement("CHARGE");     
        my @sortedChargeTag = qw ( DATE
                  TIME
                  );
        my %chargeHashTags;
        @chargeHashTags { @sortedChargeTag } = ($date,
                       $time
                        );  
        # Build the elements for Charge Tag                                     
        buildXMLElements(\@sortedChargeTag, \%chargeHashTags, $charge);
        $report-> appendChild($charge);
        $copy_person_pin = $person_pin;
    }
    else {
        my $charge = $doc->createElement("CHARGE");     
        my @sortedChargeTag = qw ( DATE
                  TIME
                  );
        my %chargeHashTags;
        @chargeHashTags { @sortedChargeTag } = ($date,
                       $time
                        );  
        # Build the elements for Charge Tag                                     
        buildXMLElements(\@sortedChargeTag, \%chargeHashTags, $charge);
        $report-> appendChild($charge); 
    }
}

$doc->setDocumentElement($root);

# Write the XML to a file
print $FILEWRITE ($doc->toString());
close $FILEWRITE;

sub buildXMLElements {
  my($elementTags, $hashTags, $parentElement) = @_;
  for my $name (@$elementTags) {
    my $reportTag = $doc->createElement($name);
    my $reportValue = $hashTags->{$name};
    $reportTag->appendTextNode($reportValue);
    $parentElement->appendChild($reportTag);
  }
}