Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Reading/Parsing an XML file using Perl

by manu_06 (Novice)
on Sep 17, 2010 at 00:08 UTC ( #860416=perlquestion: print w/ replies, xml ) Need Help??
manu_06 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have bunch of XML files where using perl I have to read each file one by one and check certain tag values and if the values do not match print the filename and tag name to an output file. My script below is checking the the tag values and printing match or not match. Now I got stuck with how to print the filename and tag name if there is no match and also instead of single file declaration a path should be declared so that it can read all the files. Can I get some help regarding this.

#!/usr/local/bin/perl-w use XML::Simple; my $xml = XML::Simple->new; my $file = $xml->XMLin('IMD025350802_000001.xml') or die$!; if (($file->{identification}{'identity'}{'format'} eq 'JPEG File Inter +change Format') && ($file->{filestatus}{'well-formed'}{'content'} eq 'true') && ($file->{ +filestatus}{'valid'}{'content'} eq 'true')) { print "match"; } else { print "not match" }

Comment on Reading/Parsing an XML file using Perl
Download Code
Re: Reading/Parsing an XML file using Perl
by toolic (Chancellor) on Sep 17, 2010 at 00:24 UTC
    You can get a list of XML files in a directory using glob. Then, loop over those files and simply print the file name.
    use strict; use warnings; use XML::Simple; my $path = '.'; # add your path here for my $xfile (glob "$path/*.xml") { print "xfile=$xfile\n"; my $xml = XML::Simple->new(); my $file = $xml->XMLin($xfile) or die $!; # do your checks... }
      Hi,

      Thank you for the help. I was trying your suggestion but it is showing up syntax error near the end of the script "}"

      #!/usr/local/bin/perl-w use strict; use warnings; use XML::Simple; my $path = 'C:\Documents and Settings\user\Desktop\output2\FITS\ IMD02 +5350802'; # add your path here for my $xfile (glob "$path/*.xml") { print "xfile=$xfile\n"; my $xml = XML::Simple->new(); my $file = $xml->XMLin($xfile) or die $!; if (($file->{identification}{'identity'}{'format'} eq 'JPEG File I +nterchange Format') && ($file->{filestatus}{'well-formed'}{'content'} eq 'true') && ($file->{ +filestatus}{'valid'}{'content'} eq 'true')) }

      Error: syntax error at one.pl line 14, near ") }"

        You're missing at least an opening brace. You have

        ($file->{filestatus}{'well-formed'}{'content'} eq 'true') && ($file->{ +filestatus}{'valid'}{'content'} eq 'true')) }

        but you need something like

        ($file->{filestatus}{'well-formed'}{'content'} eq 'true') && ($file->{ +filestatus}{'valid'}{'content'} eq 'true')) { # do something useful }
Re: Reading/Parsing an XML file using Perl
by GrandFather (Cardinal) on Sep 17, 2010 at 00:34 UTC

    The following should point you in a useful direction.

    #!/usr/local/bin/perl-w use strict; use warnings; use XML::Simple; my @files = ('IMD025350802_000001.xml'); my $xml = XML::Simple->new; for my $fileName (@files) { my $file = $xml->XMLin($fileName) or die "Failed for $fileName: $! +\n"; my $format = $file->{identification}{'identity'}{'format'}; if ($format ne 'JPEG File Interchange Format') { print "Bad format ($format) for $fileName\n"; next; } if ($file->{filestatus}{'well-formed'}{'content'} ne 'true') { print "Content not well formed for $fileName\n"; next; } if ($file->{filestatus}{'valid'}{'content'} ne 'true') { print "Content not valid for $fileName\n"; next; } }
    True laziness is hard work
      Hi,

      Thank you it is working and trying how can I read multiple files from a path instead of a single file and generate an out file instead of just printing at the command line.

        1)

        toolic posted:

        You can get a list of XML files in a directory using glob. Then, loop over those files

        2)
        use strict; use warnings; use 5.010; open my $OUTFILE, '>', 'data.txt' or die "Couldn't open data.txt: $!"; for (1 .. 10) { say {$OUTFILE} 'hello world'; } close $OUTFILE;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://860416]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-07-26 01:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls