Operating System - HP-UX
1753786 Members
7429 Online
108799 Solutions
New Discussion юеВ

Need Help in Parsing XML through XML::Simple in Perl.

 
SOLVED
Go to solution
Mayank D
Regular Advisor

Need Help in Parsing XML through XML::Simple in Perl.

Hi,

It's usrgent.I m very new to perl and xml.
I m trying to parse an xml file. Suppose the below, i cant give the original file becoz of some security reasons.




Create
20091212000000
Delete
Service


Create
20091212000000
Delete
Service



I need to get the data of Message, data, Action, Object for every Header but i m not able to find how to do that.Please tell me how to do proceeed with it.

Regards,
Mayank
6 REPLIES 6
Mayank D
Regular Advisor

Re: Need Help in Parsing XML through XML::Simple in Perl.

I have attached the sample code here and above to parse the xml which i wrote but it's not able to work with many header elements in the file, it works only for one header. Please tell me what are the things required to make parse an XML using perl.

Regards,
Mayank
Martin Vorlaender
Honored Contributor

Re: Need Help in Parsing XML through XML::Simple in Perl.

Mayank,

the Data::Dumper output should have told you...

The attached sample code gets all header elements.

HTH,
Martin
Mayank D
Regular Advisor

Re: Need Help in Parsing XML through XML::Simple in Perl.

Thanks Martin for quick reply. I have xml like this(Attached here). I tried ur code on my xml file with little modification but i could not fetch the data from it. I made some changes here as shown below to make it work with my xml file but it says "Not a hash reference at perl.pl at line 12". I m not getting what it it.
Please help me to solve it.

#!/usr/bin/perl

# use module
use XML::Simple;
use Data::Dumper;

# create object
$xml = new XML::Simple;

# read XML file
$data = $xml->XMLin("mgw.xml", ForceArray => 1, KeyAttr => []);
$PMTarget = $data->{OMeS}->{PMSetup}->{PMMOResult}-{PMTarget};
foreach my $header (@{$PMTarget})
{
print "CHECK 1";
print $PMTarget->{M617C0};
print $PMTarget->{M617C1};
print $PMTarget->{M617C2};
print $PMTarget->{M617C3};
}
Martin Vorlaender
Honored Contributor
Solution

Re: Need Help in Parsing XML through XML::Simple in Perl.

Mayank,

several things are wrong with your new code:
- the root element OMeS does not appear within the parsed object
- inside each hash there's an array of objects, so you have to insert "[0]" between accesses to hash references
- you got multiple PMMOResult elements, so your loop has to iterate over those (and not over the PMTarget elements

The attached code parses your XML file.

cu,
Martin
dirk dierickx
Honored Contributor

Re: Need Help in Parsing XML through XML::Simple in Perl.

i don't know if you already did this or not, but perl contains very good documentation. it can be accessed using the 'perldoc' command, by adding the module name you get almost everything you could want (including lots of examples).

in your case you run:
perldoc XML::Simple
Mayank D
Regular Advisor

Re: Need Help in Parsing XML through XML::Simple in Perl.

Thanks Martin for helping me to solve the issue.
Thanks Dirk for your suggestion.

Regards,
Mayank