Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading an XMl log file in perl

Posted on 2011-02-17
5
Medium Priority
?
306 Views
Last Modified: 2012-05-11
Hi,
 I am reading a set of xml files and looking for some tag and their values. If they match the fine else generate an error.

For example in the XML file . I will look for

<format> JPEG
<Well-formed > true
<valid status> true

Now I need to perform an additional check. If the tag does not exsists, I mean  for example if the <Well-formed > or <Valid Status > tags are missing it should say tag not present.

present Script:


#!/usr/local/bin/perl

#use strict;
#use warnings;
use XML::Simple;
use File::Basename;

my @InputFiles = (</usr/qalogs/BATCH0012/123498/*.xml>);

my $xml = XML::Simple->new;


foreach my $fileName(@InputFiles) {
#print "$fileName\n";

 my ($filename, $directories, $suffix) = fileparse($fileName);

 my $file = $xml->XMLin($fileName) or die "Failed for $fileName: $!
\n";
    my $format = $file->{identification}{'identity'}{'format'};

    if ($format ne 'JPEG') {

    my $dir = '/usr/summary/'.basename($filename).".out";

                           $dir =~s/(.*)_.*/$1/i;
                           mkdir $dir;


                           my $OutputFile = "$dir/Summarylog.txt";


                            open(my $OUTF, ">>$OutputFile") or die "Can't open output file ($OutputFile): $!";

        print  $OUTF  "$filename | Identity Format Error\n";
        next;
    }

    if ($file->{filestatus}{'well-formed'}{'content'} ne 'true') {

    my $dir = '/usr/summary/'.basename($filename).".out";

                               $dir =~s/(.*)_.*/$1/i;
                               mkdir $dir;


                               my $OutputFile = "$dir/Summarylog.txt";


                                open(my $OUTF, ">>$OutputFile") or die "Can't open output file ($OutputFile): $!";

        print OUTF " $filename | well formed Error\n";
        next;
    }

    if ($file->{filestatus}{'valid'}{'content'} ne 'true') {

    my $dir = '/usr/summary/'.basename($filename).".out";

                               $dir =~s/(.*)_.*/$1/i;
                               mkdir $dir;


                               my $OutputFile = "$dir/Summarylog.txt";


                                open(my $OUTF, ">>$OutputFile") or die "Can't open output file ($OutputFile): $!";

        print OUTF " $filename | Valid Status Error\n";
        next;
    }
}


0
Comment
Question by:new_perl_user
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:wilcoxon
ID: 34922790
This should do what you want...
#!/usr/local/bin/perl

#use strict;
#use warnings;
use XML::Simple;
use File::Basename;

my @InputFiles = (</usr/qalogs/BATCH0012/123498/*.xml>);

my $xml = XML::Simple->new;

foreach my $fileName(@InputFiles) {
    #print "$fileName\n";

    my ($filename, $directories, $suffix) = fileparse($fileName);

    my $file = $xml->XMLin($fileName) or die "Failed for $fileName: $!\n";
    my $format = $file->{identification}{'identity'}{'format'};

    if ($format ne 'JPEG') {

        my $dir = '/usr/summary/'.basename($filename).".out";
        $dir =~s/(.*)_.*/$1/i;
        mkdir $dir;
        my $OutputFile = "$dir/Summarylog.txt";
        open(my $OUTF, ">>$OutputFile") or die "Can't open output file ($OutputFile): $!";
        print  $OUTF  "$filename | Identity Format Error\n";
        close $OUTF;
        next;
    }

    if (not exists $file->{filestatues}{'well-formed'} or not exists $file->{filestatus}{'well-formed'}{content}) {

        my $dir = '/usr/summary/'.basename($filename).".out";
        $dir =~s/(.*)_.*/$1/i;
        mkdir $dir;
        my $OutputFile = "$dir/Summarylog.txt";
        open(my $OUTF, ">>$OutputFile") or die "Can't open output file ($OutputFile): $!";
        print OUTF " $filename | well-formed tag not present\n";
        close $OUTF;
        next;
        
    } elsif ($file->{filestatus}{'well-formed'}{'content'} ne 'true') {

        my $dir = '/usr/summary/'.basename($filename).".out";
        $dir =~s/(.*)_.*/$1/i;
        mkdir $dir;
        my $OutputFile = "$dir/Summarylog.txt";
        open(my $OUTF, ">>$OutputFile") or die "Can't open output file ($OutputFile): $!";
        print OUTF " $filename | well formed Error\n";
        close $OUTF;
        next;
    }

    if (not exists $file->{filestatus}{valid} or not exists $file->{filestatus}{valid}{content}) {

        my $dir = '/usr/summary/'.basename($filename).".out";
        $dir =~s/(.*)_.*/$1/i;
        mkdir $dir;
        my $OutputFile = "$dir/Summarylog.txt";
        open(my $OUTF, ">>$OutputFile") or die "Can't open output file ($OutputFile): $!";
        print OUTF " $filename | valid tag not present\n";
        close $OUTF;
        next;

    } elsif ($file->{filestatus}{'valid'}{'content'} ne 'true') {

        my $dir = '/usr/summary/'.basename($filename).".out";
        $dir =~s/(.*)_.*/$1/i;
        mkdir $dir;
        my $OutputFile = "$dir/Summarylog.txt";
        open(my $OUTF, ">>$OutputFile") or die "Can't open output file ($OutputFile): $!";
        print OUTF " $filename | Valid Status Error\n";
        close $OUTF;
        next;
    }
}

Open in new window

0
 

Author Comment

by:new_perl_user
ID: 34926248

Hi,
 I tried the above script but it did not write error  to the log file when the tag are missing. Below is the sample xml log file I am writing to read.

<?xml version="1.0" encoding="UTF-8"?>

<fits xmlns="http://hul.harvard.edu/ois/xml/ns/fits/fits_output" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://hul.harvard.edu/ois/xml/ns/fits/fits_output http://hul.harvard.edu/ois/xml/xsd/fits/fits_output.xsd" version="0.4.2" timestamp="2/17/11 10:46 AM">

  <identification>

    <identity format="JPEG File Interchange Format" mimetype="image/jpeg">

      <tool toolname="Jhove" toolversion="1.5" />

      <tool toolname="file utility" toolversion="4.17" />

      <tool toolname="Exiftool" toolversion="7.74" />

      <tool toolname="Droid" toolversion="3.0" />

      <tool toolname="NLNZ Metadata Extractor" toolversion="3.4GA" />

      <version toolname="Jhove" toolversion="1.5">1.01</version>

      <externalIdentifier toolname="Droid" toolversion="3.0" type="puid">fmt/43</externalIdentifier>

    </identity>

  </identification>

  <fileinfo>

    <size toolname="Jhove" toolversion="1.5">862055</size>

    <creatingApplicationName toolname="Jhove" toolversion="1.5">Canon EOS 5D Mark II</creatingApplicationName>

    <lastmodified toolname="Exiftool" toolversion="7.74" status="SINGLE_RESULT">2011:01:05 10:02:44-05:00</lastmodified>

    <created toolname="Exiftool" toolversion="7.74">2010:09:10 15:50:12</created>

    <md5checksum toolname="OIS File Information" toolversion="0.1" status="SINGLE_RESULT">c45e32f5b1f8110552462bd26199841a</md5checksum>

    <fslastmodified toolname="OIS File Information" toolversion="0.1" status="SINGLE_RESULT">1294239764000</fslastmodified>

  </fileinfo>

  <filestatus>

    <well-formed toolname="Jhove" toolversion="1.5" status="SINGLE_RESULT">true</well-formed>

    <valid toolname="Jhove" toolversion="1.5" status="SINGLE_RESULT">true</valid>

  </filestatus>
0
 
LVL 27

Accepted Solution

by:
wilcoxon earned 2000 total points
ID: 34926490
I reworked the script some to make it easier to see what's going on (mostly moving the output stuff into a sub since it was nearly identical in all 5 cases).

One problem is that the script is written to stop looking at the current file after encountering the first error.  Do you want the script to keep looking for more errors for the current file?

Another problem is that the identity check is looking for format equal JPEG but it doesn't - it starts with JPEG so I modified the script to look for that.

There was a typo in the check for well-formed not being found (fixed).

Why do you expect the script to give an error about missing tags?  I see both tags (well-formed and valid) near the bottom of your log file (and they are both true so pass that check as well).
#!/usr/local/bin/perl

use strict;
use warnings;
use XML::Simple;
use File::Basename;

my @InputFiles = (</usr/qalogs/BATCH0012/123498/*.xml>);
#my @InputFiles = ('tmp.xml');

my $xml = XML::Simple->new;

foreach my $fileName (@InputFiles) {
    #print "$fileName\n";

    my ($filename, $directories, $suffix) = fileparse($fileName);

    my $file = $xml->XMLin($fileName) or die "Failed for $fileName: $!\n";

    if ($file->{identification}{identity}{format} !~ m{^JPEG\b}) {
        output($filename, 'Identity Format Error');
        next;
    }

    if (not exists $file->{filestatus}{'well-formed'} or not exists $file->{filestatus}{'well-formed'}{content}) {
        output($filename, 'well-formed tag not present');
        next;

    } elsif ($file->{filestatus}{'well-formed'}{content} ne 'true') {
        output($filename, 'well formed Error');
        next;
    }

    if (not exists $file->{filestatus}{valid} or not exists $file->{filestatus}{valid}{content}) {
        output($filename, 'valid tag not present');
        next;

    } elsif ($file->{filestatus}{valid}{content} ne 'true') {
        output($filename, 'Valid Status Error');
        next;
    }
}

sub output {
    my ($filename, $msg) = @_;
    my $dir = '/usr/summary/'.basename($filename).'.out';
#    my $dir = 'test/'.basename($filename).'.out';
    $dir =~s/(.*)_.*/$1/i;
    unless (-d $dir) {
        mkdir $dir or die "could not mkdir $dir: $!";
    }
    my $OutputFile = "$dir/Summarylog.txt";
    open(my $OUTF, ">>$OutputFile") or die "Can't open output file ($OutputFile)
: $!";
    print  $OUTF  "$filename | $msg\n";
    close $OUTF;
}

Open in new window

0
 

Author Comment

by:new_perl_user
ID: 34928753
Hi,
 Thank you it is working now. regrading the errors.

If  it encounters one error and stop there then it is fine because even one error  indicates as an error for the whole file.

Thanks you for correcting the JPEG.

For the above log file the file status  contains both the tags but for other files they are missing so want to get hold of those files.

Thanks,

0
 
LVL 27

Expert Comment

by:wilcoxon
ID: 34928831
Good to hear.  If you run into any further problems please post a log file that exhibits the behavior and I'll debug it.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question