michael99 has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to replace some nodes value in XML using the XML:LibXML file parser from user input.
Example XML
<header> <id x_id="1"> <a></a> <b></b> <c>NA</c> </id> </header>
Code:
use strict; use warnings; use XML::LibXML; print "Please specify node c content\n"; my $node_c = <STDIN>; chomp $node_c; my $template = "xx.xml"; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($template); my($object0) = $doc->findnodes("/header/id/c/text()"); my $text0 = XML::LibXML::Text->new($node_c); $object0->replaceNode($text0);
Currently my code is working, but it somehow removed and from the xml when I tried to replace Node b content if both nodes are empty. If there is some value in node a or b, it works fine.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML replacenode
by choroba (Cardinal) on Apr 15, 2020 at 07:49 UTC | |
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 | |
|
Re: XML replacenode
by marto (Cardinal) on Apr 15, 2020 at 07:55 UTC | |
by michael99 (Acolyte) on Apr 16, 2020 at 01:04 UTC | |
by marto (Cardinal) on Apr 16, 2020 at 07:12 UTC |