I am relative new to Perl. Please check to see what I am doing wrong.

I have following input tab delimited file, which need to be built to XML, 'pin'number is the unique identifier. If the 'Pin' matches then populate show multiple 'CHARGE' under the report.

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

I want it to build following 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>

Below is the code I have

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 avalibl +e 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); } }
The output I get is, basically only the first 'CHARGE' is showing up not the other two.
<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>

In reply to Building XML with tab delimited by sannag

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.