bimleshsharma has asked for the wisdom of the Perl Monks concerning the following question:

I have a log file having xml messages and other stuffs. I want to get all xml messages from log with some condition, Example: all xml messages having book="abc".

<test> <name> 111 </name> <book> 234 </book> </test> <test> <name> 111 </name> <book> abc </book> </test>

So from above i want set of xml message having attribute "<book> abc </book>"

I tried below concept but it is not printing filtered one, instead of that it is printing all xml messages.

open(CP,"<cpsFile.log") while(<CP>) { my $line= $_; if($line =~ m{<FXSlim} .. m{</FXSlim>}){ $soapLine.=$_; } print TRADE "$soapLine";

Replies are listed 'Best First'.
Re: Fetch particular xml message from log
by choroba (Cardinal) on Sep 12, 2013 at 09:18 UTC
    Using XML::XSH2, a wrapper around XML::LibXML:
    open data.xml ; ls //test[book = " abc "] ;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Actually my question was different. Like i have a log file and having lot of blaw blaw lines as well as those xml messages. So i wanted to filter only xml messages and that script what i posted is doing but i wanted to implement some criteria to get precise xml messages from log directly. Parsing method i know but messages count is too much that is why wanted to reduce xml message crowd by implementing some condition. I hope i am clear now?

Re: Fetch particular xml message from log
by poj (Abbot) on Sep 12, 2013 at 07:52 UTC
    #!perl use strict; use XML::Twig; my $xml = q!<xml><test> <name> 111 </name><book> 234 </book> </test><test> <name> 111 </name><book> abc </book> </test></xml>!; my $t = XML::Twig->new( twig_handlers => {'test/book[string()=~/abc/]'=> \&book} ); $t->parse($xml); sub book { my($t,$book) = @_; $book->parent->print; print "\n"; }
Re: Fetch particular xml message from log (twig)
by Anonymous Monk on Sep 12, 2013 at 06:30 UTC