473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
File Type: txt Reports_pl.txt (2.0 KB, 485 views)
File Type: txt Reports_xml.txt (2.0 KB, 494 views)
Dec 15 '09 #1
4 3550
RonB
589 Expert Mod 512MB
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
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
RonB
589 Expert Mod 512MB
Expand|Select|Wrap|Line Numbers
  1. foreach my $report ( @{ $reportlist->{config}{mpid}{$mpid}{report} } ) {
Dec 17 '09 #4
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, 408 views)
Jan 9 '10 #5

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

Similar topics

4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
1
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is...
1
by: Steve Bennett | last post by:
Can anyone suggest a good tutorial for reading and writing binary files in Perl or send me some tips or examples? I tried using pack and unpack and seek and read but things don't work as expected...
3
by: Procor | last post by:
Hello, i'm new with perl. I want to read a file and display this when visiting my website : In my browser : http://www.website.nl/displayfile.pl =================== #!/usr/local/bin/perl...
1
by: Yash | last post by:
Hi, Through my VB .NET code , I am trying to invoke a perl script. My aim is to write lines to the script and read from it. With ProcInfo .Arguments = Enricher.EnrichScript + " " + enArgs...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
20
by: plmanikandan | last post by:
Hi, I need to read a file line by line.each line contains different number of characters.I opened file using fopen function.is there any function to read the file line by line Regards, Mani
14
by: noridotjabi | last post by:
Two questions. 1)Is there any way that I can read from an executable and then execute what I have read. EXAMPLE: text text this is more text
15
by: leorulez | last post by:
Is there any way to read multiple files (more than 1000 files) and then write into one single output file using C? Right now in my program, I have a loop which asks for the filename and writes into...
8
by: Jean-Marie Vaneskahian | last post by:
Reading - Parsing Records From An LDAP LDIF File In .Net? I am in need of a .Net class that will allow for the parsing of a LDAP LDIF file. An LDIF file is the standard format for representing...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.