Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: CSV file info into xml tags

by CSharma (Sexton)
on Jul 28, 2015 at 10:14 UTC ( [id://1136580]=note: print w/replies, xml ) Need Help??


in reply to Re: CSV file info into xml tags
in thread CSV file info into xml tags

Thanks POJ! To access its values, isn't this the way?
foreach $t (sort keys %sidHash){ print "$t : State " . join(", ", @{$t->{'state'}}) . "rate: " + . join(", ", @{$t->{'rate'}} . "\n"; }

Replies are listed 'Best First'.
Re^3: CSV file info into xml tags
by 2teez (Vicar) on Jul 28, 2015 at 12:20 UTC

    What about using this:

    foreach my $t ( sort keys %sidHash ) { if ( scalar @{ $sidHash{$t}{state} } > 1 ) { print map { sprintf "%s: State %s rate: %s\n" => $t, $sidHash{$t}{state}[$_], $sidHash{$t}{rate}[$_] } 0 .. $#{ $sidHash{$t}{state} }; } else { print "$t: State ", join( ", " => @{ $sidHash{$t}{state} } ), " rate: ", join( ", " => @{ $sidHash{$t}{rate} } ), $/; }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Thanks 2teez! I've one problem, my requirement is to integrate 'StateTax' tag in xml file which I'm creating using different files. I've got 2 hashes: 1st -> data having various fields which is to be added into xml file. Hash is like below:
      $VAR1 = { 'retailer-2247.txt.gz' => { 'logo' => 'http://cfsi.pgcdn.com +/images/retbutton_2247.gif', 'mid' => '2247', 'feedCount' => undef, 'feedNumbers' => undef, 'url' => 'http://tracking.search +marketing.com', 'merchant' => '1STOPlighting', 'type' => 'CPC', 'parentAccount' => undef, 'feedName' => 'retailer-2247.txt +.gz' } }
      2nd -> tax info retrieved from a different file. Hash is like:
      { '19428' => { 'rate' => [ '7.000' ], 'state' => [ 'NJ' ] }, '2247' => { 'rate' => [ '9.750', '7.000' ], 'state' => [ 'IL', 'IN' ] } }
      So I need to merge the state info tag into xml file (comparison would be on mid i.e. 2247). Could anyone please help? And this question regarding the same I've asked earlier.
      <merchant id="2247"> <name>1STOPlighting</name> <url>http://tracking.searchmarketing.com</url> <type>CPC</type> <logoUrl>http://cfsi.pgcdn.com/images/retbutton_2247.gif</logoUrl> <ntParentAcct>5403020</ntParentAcct> <feedCount>1</feedCount> <feedNumbers>1</feedNumbers> <StateTax>IL, IN</StateTax> </code PERL Code I've written, it's using XML::LibXML module to handle encodi +ng cases. <code> #!/usr/bin/perl use Net::SFTP::Foreign; use IO::Uncompress::Gunzip qw(gunzip $GunzipError); use URI::Escape; use Net::FTP; use XML::LibXML; use Data::Dumper; $mdFile = "meta.xml"; my @feedFiles = <*.gz>; $fileRef = \@feedFiles; @cpaAccts = ("402", "1888", "2379", "5316", "12968", "24379", "25101", + "25518"); my $taxFile = "taxinfo.txt"; ## Getting hash of taxFile my %midHash = &readTax($taxFile); ## Getting hash of metaData &writeXml(@$fileRef); print Dumper(\%metaHash); ## subroutine to get the hash of TaxFile sub readTax { my ($taxFile) = @_; my %sidHash=(); open(IN, $taxFile) || die "$taxFile couldn't be opened $@\n"; while(<IN>) { chomp; next if /Retid/; my ($sid, $state, $rate) = split /\s+/; push @{$sidHash{$sid}{state}}, $state; push @{$sidHash{$sid}{rate}} , $rate; } return %sidHash; } ## subroutine to get the hash of feeds pgmetadata.xml sub writeXml { @feeds = @_; # Read the cpa accts into a hash %cpaHash = map { $cpaAccts[$_] => 1 } (0..$#cpaAccts); foreach $fr (@feeds) { $f = $fr; open(IN,sprintf("zcat %s |", $f)) || die "Could not open pipe +for $f : $!"; while(<IN>) { next if $. == 1; if ($. == 2) { @feedLine = split('\t'); $metaHash{$f}{feedName} = $f; $metaHash{$f}{mid} = $feedLine[0]; if ($cpaHash{$feedLine[0]} > 0) { $metaHash{$f}{type} = 'CPA'; } else { $metaHash{$f}{type} = 'CPC'; } $feedLine[4] =~ s/_/ /g; $metaHash{$f}{merchant} = $feedLine[4]; $metaHash{$f}{logo} = $feedLine[5]; $tempUrl = uri_unescape($feedLine[18]); ($metaHash{$f}{url}) = $tempUrl =~ /.*(http[s]?:\/\/[^ +\/\?&]+)/; $metaHash{$f}{parentAccount} = $parentAccount; $metaHash{$f}{feedCount} = $feedCount; $metaHash{$f}{feedNumbers} = $feedNumbers; last; } } close IN; } return %metaHash; } ## Subroutine to create XML file sub writeMetaDataFile { my (%tmpMetaHash) = @_; open(OUTXML,">$mdFile") || die "Could not open $mdFile for Writing +"; $doc = XML::LibXML::Document->new(); $root = $doc->createElement('merchants'); $doc->setDocumentElement($root); foreach $k (sort keys %tmpMetaHash) { my $merchant = $doc->createElement('merchant'); $merchant->setAttribute('id',$tmpMetaHash{$k}{mid}); $root->appendChild($merchant); $merchant->appendTextChild('name',$tmpMetaHash{$k}{merchant}); $merchant->appendTextChild('url',$tmpMetaHash{$k}{url}); $merchant->appendTextChild('type',$tmpMetaHash{$k}{type}); $merchant->appendTextChild('logoUrl',$tmpMetaHash{$k}{logo}); $merchant->appendTextChild('ntParentAcct',$parentAcct); $merchant->appendTextChild('feedCount',$feedCount); $merchant->appendTextChild('feedNumbers',$feedNumbers); } print OUTXML $doc->toString(1); push @ftpFiles, $mdFile; }

        Add these lines to writeMetaDataFile

        my $rate = join ',',@{$taxHash->{$id}{state}}; $merchant->appendTextChild('StateTax', $rate);

        Full script below

        poj
Re^3: CSV file info into xml tags
by CSharma (Sexton) on Jul 28, 2015 at 10:33 UTC
    Have got it, accessed this way!
    foreach my $t (sort keys %midHash){ print $t . "\t" . join(", ", @{$midHash{$t}{state}}) . "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1136580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 03:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found