in reply to XML replacenode
If you want to fill a node regardless of whether it contains any text, don't use its text contents - as there can be none. Clean the node instead and add the new text:
#! /usr/bin/perl use strict; use feature qw{ say }; use warnings; use XML::LibXML; my $xml = '<header> <id x_id="1"> <a></a> <b></b> <c>NA</c> </id> </header>'; print "Please specify node c content: "; chomp( my $new_text = <STDIN> ); my $dom = 'XML::LibXML'->load_xml(string => $xml); for my $node($dom->findnodes('(/header/id/b | /header/id/c)')) { $node->removeChildNodes; $node->appendText($new_text); } print $dom;
Or, try XML::XSH2:
open file.xml ; for (/header/id/b|/header/id/c) set text() "YOUR TEXT HERE" ; save :b ;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML replacenode
by michael99 (Acolyte) on Apr 16, 2020 at 01:02 UTC | |
by michael99 (Acolyte) on Apr 16, 2020 at 01:15 UTC | |
by hippo (Archbishop) on Apr 16, 2020 at 08:07 UTC |