in reply to XML replace password with a specific tag

Here's one way with XML::LibXML:

use warnings; use strict; use XML::LibXML; my $doc = XML::LibXML->load_xml(location => 'foo.xml'); my @nodes = $doc->findnodes('//param[@name="SSLKeyStorePassword"]'); if (@nodes) { warn "found more than one SSLKeyStorePassword" if @nodes>1; $nodes[0]->{value} = 'BLABLA8'; } else { die "did not find SSLKeyStorePassword" } $doc->toFile('bar.xml', 1);

Whatever you do, do not use regular expresions.

Replies are listed 'Best First'.
Re^2: XML replace password with a specific tag
by choroba (Cardinal) on Oct 14, 2022 at 08:10 UTC
    A bit shorter in XML::XSH2:
    open foo.xml ; set /Listens/Listen/param[@name="SSLKeyStorePassword"]/@value 'BLABLA8 +' ; save :b ;

    It sets the first such node, i.e. it doesn't warn if there are more than one, but it can be easily extended:

    my $l = /Listens/Listen/param[@name="SSLKeyStorePassword"] ; if (count($l) > 1) echo :e 'More than one node found' ; set $l/@value 'BLABLA8' ;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]