http://qs321.pair.com?node_id=1231126

Lotus1 has asked for the wisdom of the Perl Monks concerning the following question:

I found an example of replacing XML text nodes by using removeChildNodes() and appendText() in case there are zero or more than one text node so I started doing it that way. Now I need to replace the value of a comment at the root node of my XML document so this won't work. I seem to remember that comments don't have children so it seems safe to just use setData(). Is this a valid assumption? I suppose an option would be to remove the comment and then create a new comment.

#Update the text in the comment. my ($first_comment) = $doc->findnodes("/comment()[1]"); my $timestamp = POSIX::strftime("%Y%m%d%H%M%S", localtime); my $new_comment = qq(Saved by $0 on $timestamp from $ENV{COMPU +TERNAME} by user $ENV{USERNAME}); $first_comment->setData($new_comment); #Update the regular text node. my ($Appname_node) = $doc->findnodes("/Testing/name1/Appname") +; $Appname_node->removeChildNodes(); $Appname_node->appendText($0);

Edit: This was just my misunderstanding of text nodes and overthinking it. Using removeChildNodes() and appendText() is fine for changing text nodes in case there is no text node. Otherwise my XML won't have the problem of multiple text nodes to worry about. My example below showed me that the text nodes created by appendTextNode() all get combined unless they are separated by an element. In the past I tested for an existing text node before either updating or doing setData. Comments can't have child elements so there can't be multiple text nodes.

use strict; use warnings; use XML::LibXML; my $doc = XML::LibXML->load_xml(string => <<END_XML); <TT> <A>xyz<B>test1</B></A> <!-- my comment --> </TT> END_XML # Print everything my ($node_a) = $doc->findnodes("/TT/A"); $node_a->appendTextNode('-123-'); $node_a->appendTextNode('-456-'); print $doc->toString, "\n"; my $count=1; foreach($doc->findnodes("/TT/A/text()")){ print $count++,": ", $_->data, "\n"; } __END__ <?xml version="1.0"?> <TT> <A>xyz<B>test1</B>-123--456-</A> <!-- my comment --> </TT> 1: xyz 2: -123--456-