Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

Reading an XMl log file in perl

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
new_perl_user
Asked:
new_perl_user
  • 3
  • 2
1 Solution
 
wilcoxonCommented:
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
 
new_perl_userAuthor Commented:

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
 
wilcoxonCommented:
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
 
new_perl_userAuthor Commented:
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
 
wilcoxonCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now