How can I find and replace text in XML using Perl?

后端 未结 3 1304
天涯浪人
天涯浪人 2021-01-15 06:33

My XML file looks something like this:


    
        
               
             


        
3条回答
  •  攒了一身酷
    2021-01-15 07:07

    Assuming that your XML is well formed (it isn't) you can use a number of CPAN modules for the job. Most of the will involve parsing the document, finding your bit with an XPath query, and printing the document out again.

    Here's an example with XML::Twig. I had to fix up the XML to get it to parse.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use XML::Twig;
    
    my $twig = XML::Twig->new(
        twig_handlers => {
            'conf/prop' => sub { $_->{att}{val} =~ s/a.org/b.org/; }
        },
        pretty_print => "indented"
    );
    $twig->parse(join "", );
    
    $twig->print;
    
    
    __END__
    
    
       
              
       
    
    
       
              
       
    
    
      
              
       
    
    
    

提交回复
热议问题