Reading xml in perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dhstein
    New Member
    • Dec 2008
    • 2

    #1

    Reading xml in perl

    I'm trying to loop through an xml file to process each line which represents a report to be generated. I am stuck. I am attaching the xml file and the perl script. Rename Reports_xml.txt to Reports.xml and rename Reports_pl.txt to Reports.pl. If you can help, please email me at <email removed> Thanks.

    David
    Attached Files
    Last edited by Frinavale; Dec 17 '09, 09:54 PM. Reason: Please do not post your email address.
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    What are you stuck on?

    This is the output I get from your script. Is it not what you expect?
    mpid is MNOP
    15
    mpid is ABCD
    1
    mpid is IJKL
    9
    mpid is EFGH
    5

    Comment

    • dhstein
      New Member
      • Dec 2008
      • 2

      #3
      Thanks - but there's more

      Thanks Ron. But I'm trying to read each record. There are 8 reports in the xml file and the output doesn't give them all. That's my problem. I just want to figure out how to get perl to display the "when" entry , for example, for each of the 8 reports.

      David

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Code:
        foreach my $report ( @{ $reportlist->{config}{mpid}{$mpid}{report} } ) {

        Comment

        • zadrfle
          New Member
          • Jan 2010
          • 1

          #5
          Perl scrpt to get complete report

          If you still have this requirement, try this:

          [code=perl]
          use strict;
          use XML::Simple;
          use Data::Dumper;

          my $dump_file = "test_dump.txt" ;
          my $output_file = "Test_Outpt.txt ";

          ### XMLin seems to be happiest with a complete path.
          my $xml = XMLin('D:\Proje cts\Ad_Hoc\Repo rts.xml', forcearray => [ 'report' ]);

          ### this is very helpful in seeing how the XML is set up in memory
          open(TESTDUMP," >$dump_file") or die "Cannot open $dump_file for writing: $!\n";
          print TESTDUMP Dumper $xml;
          close TESTDUMP;

          open(OUTPUT,">$ output_file") or die "Cannot open $output_file for writing: $!\n";

          my $report_number = 1;

          foreach(@{$$xml {mpid}}) ### iterate through the mpid's
          {
          my $hash_ref = $_;
          my $id = $$hash_ref{ID}; ### save the mpid for later use

          foreach(@{$$has h_ref{report}}) ### iterate through the reports under each mpid
          {

          my $report_hash_re f = $_;

          ### format & print however you like
          print OUTPUT "Report Number: $report_number\ n";
          $report_number+ +;
          print OUTPUT "\tmpid: $id\n";
          print OUTPUT "\temdail ID: $$report_hash_r ef{emailID}\n";
          print OUTPUT "\twhen: $$report_hash_r ef{when}\n";
          print OUTPUT "\ttype: $$report_hash_r ef{type}\n";
          print OUTPUT "\ttime: $$report_hash_r ef{time}\n";
          print OUTPUT "\n============ =============== ======\n\n";

          }; ### end of foreach(@{$$has h_ref{report})


          }; ### end of foreach(@{$xml{ mpid}})

          close OUTPUT;
          [/code]
          Attached Files

          Comment

          Working...