Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Reading an XML tag in Perl

by Jaap (Curate)
on Apr 08, 2011 at 14:08 UTC ( #898371=note: print w/replies, xml ) Need Help??


in reply to Reading an XML tag in Perl

Please show your corresponding piece of code.
You should probably do something like $file->{toolOutput}->getAttribute('xmlns') depending on what module you are using.

Replies are listed 'Best First'.
Re^2: Reading an XML tag in Perl
by Anonymous Monk on Apr 08, 2011 at 14:28 UTC

    Hi, I am reading an XML log file and comparing the tag values if they match then fine else need to generate an error log file.



    .
    xml log file <?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="3/2 +2/11 9:36 AM"> <identification status="CONFLICT"> <identity format="TIFF Baseline grayscale (Class G)" mimetype="ima +ge/tiff"> <tool toolname="Jhove" toolversion="1.5" /> <version toolname="Jhove" toolversion="1.5">6.0</version> </identity> <identity format="Tagged Image File Format" mimetype="image/tiff"> <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" /> <tool toolname="ffident" toolversion="0.2" /> <version toolname="Exiftool" toolversion="7.74" status="CONFLICT +">2.1.0</version> <version toolname="Droid" toolversion="3.0" status="CONFLICT">3< +/version> <version toolname="Droid" toolversion="3.0" status="CONFLICT">4< +/version> <version toolname="Droid" toolversion="3.0" status="CONFLICT">5< +/version> <version toolname="Droid" toolversion="3.0" status="CONFLICT">6< +/version> <externalIdentifier toolname="Droid" toolversion="3.0" type="pui +d">fmt/7</externalIdentifier> <externalIdentifier toolname="Droid" toolversion="3.0" type="pui +d">fmt/8</externalIdentifier> <externalIdentifier toolname="Droid" toolversion="3.0" type="pui +d">fmt/9</externalIdentifier> <externalIdentifier toolname="Droid" toolversion="3.0" type="pui +d">fmt/10</externalIdentifier> </identity> </identification> <fileinfo> <size toolname="Jhove" toolversion="1.5">5978612</size> <creatingApplicationName toolname="Jhove" toolversion="1.5">Adobe +Photoshop CS3 Windows</creatingApplicationName> <lastmodified toolname="Exiftool" toolversion="7.74" status="SINGL +E_RESULT">2011:03:22 09:20:40-04:00</lastmodified> <created toolname="Exiftool" toolversion="7.74" status="SINGLE_RES +ULT">2011:03:11 09:18:36-05:00</created> <filename toolname="OIS File Information" toolversion="0.1" status +="SINGLE_RESULT"></filename> <md5checksum toolname="OIS File Information" toolversion="0.1" sta +tus="SINGLE_RESULT">a59a63c5ed7a04625840f324c22afbc4</md5checksum> <fslastmodified toolname="OIS File Information" toolversion="0.1" +status="SINGLE_RESULT">1300800040000</fslastmodified> </fileinfo> <filestatus> <well-formed toolname="Jhove" toolversion="1.5" status="SINGLE_RES +ULT">true</well-formed> <valid toolname="Jhove" toolversion="1.5" status="SINGLE_RESULT">t +rue</valid> </filestatus> <metadata> <image> <byteOrder toolname="Jhove" toolversion="1.5" status="SINGLE_RES +ULT">little endian</byteOrder> <compressionScheme toolname="Jhove" toolversion="1.5" status="SI +NGLE_RESULT">Uncompressed</compressionScheme> <imageWidth toolname="Jhove" toolversion="1.5" status="SINGLE_RE +SULT">1839</imageWidth> <imageHeight toolname="Jhove" toolversion="1.5" status="SINGLE_R +ESULT">3239</imageHeight> <colorSpace toolname="Jhove" toolversion="1.5" status="SINGLE_RE +SULT">BlackIsZero</colorSpace> <orientation toolname="Jhove" toolversion="1.5" status="SINGLE_R +ESULT">normal*</orientation> <samplingFrequencyUnit toolname="Jhove" toolversion="1.5" status +="SINGLE_RESULT">in.</samplingFrequencyUnit> <xSamplingFrequency toolname="Jhove" toolversion="1.5" status="S +INGLE_RESULT">400</xSamplingFrequency> <ySamplingFrequency toolname="Jhove" toolversion="1.5" status="S +INGLE_RESULT">400</ySamplingFrequency> <bitsPerSample toolname="Jhove" toolversion="1.5" status="SINGLE +_RESULT">8</bitsPerSample> <samplesPerPixel toolname="Jhove" toolversion="1.5" status="SING +LE_RESULT">1</samplesPerPixel> <imageProducer toolname="Jhove" toolversion="1.5" status="SINGLE +_RESULT"></imageProducer> <scanningSoftwareName toolname="Jhove" toolversion="1.5" status= +"SINGLE_RESULT">Adobe Photoshop CS3 Windows</scanningSoftwareName> </image> </metadata> <toolOutput xmlns=""> <tool name="Jhove" version="1.5"> <repInfo uri="/usr/qa/mhf_dev/Greyscale/BATCH_0001/0020R/IMAGES/ +0020R_000002.tif"> <reportingModule release="1.5" date="2007-10-02">TIFF-hul</rep +ortingModule> <lastModified>2011-03-22T09:20:40-04:00</lastModified> <size>5978612</size> <format>TIFF</format> <version>6.0</version> <status>Well-Formed and valid</status> <sigMatch> <module>TIFF-hul</module> </sigMatch> <mimeType>image/tiff</mimeType> <profiles> <profile>Baseline grayscale (Class G)</profile> <profile>TIFF/IT-MP (ISO 12639:1998)</profile> <b> <profile>DLF Benchmark for Faithful Digital Reproductions + of Monographs and Serials: grayscale and white</profile> <b> </profiles> <properties> <property> <name>TIFFMetadata</name> <values arity="Array" type="Property"> <property> <name>ByteOrder</name> <values arity="Scalar" type="String"> <value>little-endian</value> </values> </property> <property> <name>IFDs</name> <values arity="List" type="Property"> <property> <name>Number</name> <values arity="Scalar" type="Integer"> <value>2</value> </values> </property> <property> <name>IFD</name> <values arity="Array" type="Property"> <property> <name>Offset</name> <values arity="Scalar" type="Long"> <value>8</value> </values> </property> <property> <name>Type</name> <values arity="Scalar" type="String"> <value>TIFF</value> </values> </property> <property> <name>Entries</name> <values arity="List" type="Property"> <property> <name>NisoImageMetadata</name> <values arity="Scalar" type="NISOImageMeta +data"> <value> <mix:mix xmlns:mix="http://www.loc.gov +/mix/" xsi:schemaLocation="http://www.loc.gov/mix/ http://www.loc.gov +/mix/mix02.xsd"> <mix:BasicImageParameters> <mix:Format> <mix:MIMEType>image/tiff</mix:MI +METype> <mix:ByteOrder>little-endian</mi +x:ByteOrder> <mix:Compression> <mix:CompressionScheme>1</mix: +CompressionScheme> </mix:Compression> <mix:PhotometricInterpretation> <mix:ColorSpace>1</mix:ColorSp +ace> </mix:PhotometricInterpretation> <mix:Segments> <mix:StripOffsets>22046</mix:S +tripOffsets> <mix:RowsPerStrip>3239</mix:Ro +wsPerStrip> <mix:StripByteCounts>5956521</ +mix:StripByteCounts> </mix:Segments> <mix:PlanarConfiguration>1</mix: +PlanarConfiguration> </mix:Format> <mix:File> <mix:Orientation>1</mix:Orientat +ion> </mix:File> </mix:BasicImageParameters> <mix:ImageCreation> <mix:ImageProducer>Backstage Libra +ry Works for NLM</mix:ImageProducer> <mix:ScanningSystemCapture> <mix:ScanningSystemHardware> <mix:ScannerManufacturer>Nexts +can</mix:ScannerManufacturer> <mix:ScannerModel> <mix:ScannerModelName>Eclips +e Rollfilm</mix:ScannerModelName> </mix:ScannerModel> </mix:ScanningSystemHardware> <mix:ScanningSystemSoftware> <mix:ScanningSoftware>Adobe Ph +otoshop CS3 Windows</mix:ScanningSoftware> </mix:ScanningSystemSoftware> </mix:ScanningSystemCapture> <mix:DateTimeCreated>2011-03-11T13 +:37:58</mix:DateTimeCreated> </mix:ImageCreation> <mix:ImagingPerformanceAssessment> <mix:SpatialMetrics> <mix:SamplingFrequencyUnit>2</mi +x:SamplingFrequencyUnit> <b><mix:XSamplingFrequency>400</m +ix:XSamplingFrequency><b> <b><mix:YSamplingFrequency>400</ +mix:YSamplingFrequency><b> <mix:ImageWidth>1839</mix:ImageW +idth> <mix:ImageLength>3239</mix:Image +Length> </mix:SpatialMetrics>
    Perl Script #!/usr/local/bin/perl use strict; use warnings; use XML::Simple; use File::Basename; my @InputFiles = (</usr/lqa/logs/*.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->{toolOutput}{xmlns}{format} ne 'TIFF') { output($filename, 'Identity Format Error'); next; } if ( not exsits $file->{toolOutput}{version} ne '6.0'){ output($filename, 'Version Error'); next; } if ( not exsits $file->{toolOutput}{status} ne 'Well-Formed and valid' +){ output($filename, 'Version Error'); next; } if ( not exsits $file->{profiles}{profile} ne 'DLF Benchmark for F +aithful Digital Reproductions of Monographs and Serials: grayscale and white'){ output($filename, 'Profile Error'); next; } #if ( not exsits $file->{mix:ImagingPerformanceAssessment}{mix:XSa +mplingFrequency} ne '400'){ # output($filename, 'X Error'); # next; # if ( not exsits $file->{mix:ImagingPerformanceAssessment}{mix:YSa +mplingFrequency} ne '400'){ # output($filename, 'Y 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 ($O +utputFile) : $!"; print $OUTF "$filename | $msg\n"; close $OUTF; }
      Try this
      if ($file->{toolOutput}{xmlns} eq '') { print "Nothing"; next; } if ($file->{toolOutput}{xmlns} eq 'TIFF') { print "TIFF"; next; }
      Any way are you validating XML. If not why module we can easily do it in REGEX.

        If module, why regex?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2020-09-26 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (142 votes). Check out past polls.

    Notices?