Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (3)
    As of 2021-04-21 02:48 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found