Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

best practice for replacing XML comment text

by Lotus1 (Vicar)
on Mar 11, 2019 at 19:05 UTC ( #1231126=perlquestion: print w/replies, xml ) Need Help??

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-

Replies are listed 'Best First'.
Re: best practice for replacing XML comment text
by Marshall (Canon) on Mar 12, 2019 at 00:42 UTC
    What module are you using to process HTML?
    Can you post a very small HTML example that demo's your problem?

      I edited the OP after your post to show an example I created that helped me understand the part I was having trouble with. As you can see it isn't HTML but XML. The documents I work with represent electrical diagrams that are created by a graphical drawing program.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1231126]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2021-01-25 18:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?