Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Search and replace in portion of xml file

by sandy1028 (Sexton)
on Jun 01, 2009 at 11:07 UTC ( [id://767226] : perlquestion . print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Search and replace in portion of xml file
by toolic (Bishop) on Jun 01, 2009 at 12:04 UTC
    The sensible way to modify an XML file is to use a parser, such as XML::Twig.
      ... and I'm finally getting around to providing an example today:
      use strict; use warnings; use XML::Twig; my $xmlStr = <<XML; <foo> <article>ME ME ME</article> <particle>ME TOO</particle> </foo> XML my $twig= new XML::Twig( twig_handlers => { article => \&article } ); $twig->parse($xmlStr); $twig->print(); print "\n"; exit; sub article { my ($twig, $art) = @_; my $stuff = $art->text(); # get the text of article $stuff =~ s/ME/E/g; $art->set_text($stuff); } __END__ <foo><article>E E E</article><particle>ME TOO</particle></foo>
Re: Search and replace in portion of xml file
by apl (Monsignor) on Jun 01, 2009 at 12:04 UTC
Re: Search and replace in portion of xml file
by Jenda (Abbot) on Jun 01, 2009 at 22:22 UTC
    use XML::Rules; my $parser = XML::Rules->new( style => 'filter', rules => { article => sub { my ($tag,$attr) = @_; $attr->{_content} =~ s/EM/E/g; return $tag => $attr; } } ); $parser->filterfile( 'a.xml', 'b.xml');

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Thanks for all the replies. I am trying to replace the text ctrl+O which is ^O in the file.
      open FH,'a.txt'; @data=<FH>; foreach $dat (@data){ $dat =~ s/\^O/ /g; }
      I am trying to replace the control characters with a space but no luck. Please can you suggest me how to proceed

        Perl doesn't understand what do you mean by that \^O. Try \x0F instead.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: Search and replace in portion of xml file
by Anonymous Monk on Jun 01, 2009 at 11:22 UTC
    Make an effort.
Re: Search and replace in portion of xml file
by kennethk (Abbot) on Jun 01, 2009 at 14:25 UTC
    Your best bet, as toolic and apl suggest, is to use an XML parser. Manually parsing XML is fraught with potential bugs and edge cases. However, assuming your XML is well-formed, you can accomplish this task using Look Around Assertions. Specifically, if you assume that your text field contains no < characters, you can do it with

    s/EM(?=[^<]*</article>)/replacement/g.

    This will match and replace any occurence of the letters EM that are followed by any number of non-< characters and then a closing article tag.