in reply to Re^2: XML replacenode
in thread XML replacenode
Output:#! /usr/bin/perl use strict; use feature qw{ say }; use warnings; use XML::LibXML; print "Please specify node c content: "; chomp( my $new_text = <STDIN> ); my $file = 'xx.xml'; my $dom = 'XML::LibXML'->load_xml(location => $file); for my $node($dom->findnodes('(/header/id/c)')) { $node->removeChildNodes; $node->appendText($new_text); } #print $dom; open (my $Output, "> $file") or die "Could not write $file"; print $Output $dom->toString(); close ($Output) or die "Could not close generated $file ";
Expecting output:<?xml version="1.0"?> <header> <id x_id="1"> <a/> <b/> <c>as</c> </id> </header>
<?xml version="1.0"?> <header> <id x_id="1"> <a></a> <b></c> <c>something</c> </id> </header>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: XML replacenode
by hippo (Archbishop) on Apr 16, 2020 at 08:07 UTC |