Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Finding max value from a unique tag from XML

by remiah (Hermit)
on Nov 22, 2012 at 10:45 UTC ( #1005104=note: print w/replies, xml ) Need Help??

in reply to Finding max value from a unique tag from XML

Hello Shaveta_Chawla.

It seems your xml have some trouble like '<\doc>'.
If it is valid xml, XML::Twig will do like this.

#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $xml=join('', <DATA>); my $max_docid=0; XML::Twig->new( twig_roots =>{ 'str[@name="docuid"]' => \&set_max_docid, }, )->parse($xml); sub set_max_docid { my ($twig, $elt)= @_; my $docid= $elt->first_child_trimmed_text; if ($docid > $max_docid ){ $max_docid=$docid } } print "max docid=$max_docid\n"; __DATA__ <all> <doc> <date name="processingtime">2011-04-09T11:12:22.049Z</date> <str name="docuid">121422</str> <str name="title">ABC</str> </doc> <doc> <date name="processingtime">2012-04-09T11:12:22.049Z</date> <str name="docuid">13427</str> <str name="title">CDE</str> </doc> <doc> <date name="processingtime">2010-04-09T11:12:22.049Z</date> <str name="docuid">89822</str> <str name="title">LKK</str> </doc> </all>
XML::Twig has nice tutorial.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005104]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-07-26 01:16 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (383 votes). Check out past polls.