Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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-

In reply to best practice for replacing XML comment text by Lotus1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2021-05-13 23:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (145 votes). Check out past polls.

    Notices?