Perl code for Find and replace a tag value in XML

后端 未结 3 433

Below is the XML I will be using:

ABC

相关标签:
3条回答
  • 2021-01-14 16:02

    XPath is able to find nodes without needing to know the position in the tree.

    use strictures;
    use XML::LibXML qw();
    my $dom = XML::LibXML->load_xml(string => <<'XML');
    <a>
    <id>ABC</id>
    <class />
    <gender />
    </a>
    XML
    
    for my $id ($dom->findnodes('//id[string()="ABC"]')) {
        $id->removeChildNodes;
        $id->appendText('DEF');
    }
    
    print $dom->toString
    
    0 讨论(0)
  • 2021-01-14 16:09

    A simple XML::Twig solution would be:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use XML::Twig;
    
    my $FILE= 'id.xml';
    
    XML::Twig->new( twig_handlers => { 'id[string()="ABC"]' => sub { $_->set_text( 'DEF'); } },
                    keep_spaces => 1,
                  )
              ->parsefile( $FILE)
              ->print_to_file( $FILE);
    

    Just for fun: an efficient XML::Twig solution is a bit more convoluted, mostly because you can't use string() conditions with twig_roots. Still, it's quite compact, and it never loads the whole file in memory.

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use XML::Twig;
    
    XML::Twig->new( twig_roots =>    { id => sub { $_->flush }, },
                    twig_handlers => { 'id[string()="ABC"]' => sub { $_->set_text( 'DEF'); } },
                    twig_print_outside_roots => 1,
                  )
              ->parsefile_inplace( 'id.xml');
    
    0 讨论(0)
  • 2021-01-14 16:12

    perl -i.bak -pe 's/abc/def/g' myfile.xml

    you'll get a new file myfile.xml.bak which ha

    0 讨论(0)
提交回复
热议问题