473,789 Members | 2,547 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading xml in perl

2 New Member
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, 494 views)
File Type: txt Reports_xml.txt (2.0 KB, 502 views)
Dec 15 '09 #1
4 3583
RonB
589 Recognized Expert Moderator Contributor
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
dhstein
2 New Member
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 Recognized Expert Moderator Contributor
Expand|Select|Wrap|Line Numbers
  1. foreach my $report ( @{ $reportlist->{config}{mpid}{$mpid}{report} } ) {
Dec 17 '09 #4
zadrfle
1 New Member
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, 415 views)
Jan 9 '10 #5

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

Similar topics

4
3069
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
20467
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 pressed (without hitting return). I looked at serveral faq's on the net and installed the cspan readkey module and neither seems to work most likey its me since I am getting frustrated. but anyway here's a sample code. while (1) { if...
1
5948
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 so I must be missing some subtle details. For example: I tried reading a .RAW image file (no header info) and writing it back out using seek read and print. It almost worked but any part of the image that was black
3
4639
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 $logurl = "/opt/guide/www.website.nl/log.txt";
1
1480
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 .RedirectStandardInput = True .RedirectStandardOutput = True .RedirectStandardError = True .UseShellExecute = False .CreateNoWindow = True
4
15079
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 this program returns. I'm just missing the stepshere. I know that I can set the Shell command to an integer,but this only returns a 0 to me telling me that it executed, notwhat is being returned to the console by that application. Isthere a way to...
20
2648
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
1676
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
15077
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 the output file but this is tedious. Imagine typing 1000 filenames...is there a efficient way to do this?? Thanks
8
4431
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 LDAP objects. I need to be able to read the records from an LDIF file into ..Net. There exists a Perl module that will do exactly this called Net::LDAP::LDIF but I am wanting to port my code over to .Net and cannot find anything with similar...
0
9506
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4089
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.