By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,934 Members | 1,685 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,934 IT Pros & Developers. It's quick & easy.

Reading xml in perl

P: 2
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
File Type: txt Reports_pl.txt (2.0 KB, 404 views)
File Type: txt Reports_xml.txt (2.0 KB, 403 views)
Dec 15 '09 #1
Share this Question
Share on Google+
4 Replies


Expert Mod 100+
P: 589
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
Dec 16 '09 #2

P: 2
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
Dec 16 '09 #3

Expert Mod 100+
P: 589
Expand|Select|Wrap|Line Numbers
  1. foreach my $report ( @{ $reportlist->{config}{mpid}{$mpid}{report} } ) {
Dec 17 '09 #4

P: 1
If you still have this requirement, try this:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use XML::Simple;
  3. use Data::Dumper;
  4.  
  5. my $dump_file = "test_dump.txt";
  6. my $output_file = "Test_Outpt.txt";
  7.  
  8. ### XMLin seems to be happiest with a complete path.
  9. my $xml = XMLin('D:\Projects\Ad_Hoc\Reports.xml', forcearray => [ 'report' ]);
  10.  
  11. ### this is very helpful in seeing how the XML is set up in memory
  12. open(TESTDUMP,">$dump_file") or die "Cannot open $dump_file for writing: $!\n";
  13. print TESTDUMP Dumper $xml;
  14. close TESTDUMP;
  15.  
  16. open(OUTPUT,">$output_file") or die "Cannot open $output_file for writing: $!\n";
  17.  
  18. my $report_number = 1;
  19.  
  20. foreach(@{$$xml{mpid}})                ### iterate through the mpid's
  21. {
  22.     my $hash_ref = $_;
  23.     my $id = $$hash_ref{ID};        ### save the mpid for later use
  24.  
  25.     foreach(@{$$hash_ref{report}})    ### iterate through the reports under each mpid
  26.     {
  27.  
  28.         my $report_hash_ref = $_;
  29.  
  30.         ### format & print however you like
  31.         print OUTPUT "Report Number: $report_number\n";
  32.         $report_number++;
  33.         print OUTPUT "\tmpid: $id\n";
  34.         print OUTPUT "\temdail ID: $$report_hash_ref{emailID}\n";
  35.         print OUTPUT "\twhen: $$report_hash_ref{when}\n";
  36.         print OUTPUT "\ttype: $$report_hash_ref{type}\n";
  37.         print OUTPUT "\ttime: $$report_hash_ref{time}\n";
  38.         print OUTPUT "\n=================================\n\n";
  39.  
  40.     };    ### end of foreach(@{$$hash_ref{report})
  41.  
  42.  
  43. };    ### end of foreach(@{$xml{mpid}})
  44.  
  45. close OUTPUT;
  46.  
Attached Files
File Type: txt test_pl.txt (1.3 KB, 338 views)
Jan 9 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.