Edit XML comment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingddd
Forum Newbie
Posts: 2
Joined: Tue Aug 09, 2011 12:10 pm

Edit XML comment

Post by kingddd »

Hi,
I have this XML file

Code: Select all

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Created: 8/5/2011 7:04:09 PM-->
<site>
  <Visit User="2" DateStamp="08.06.11" TimeStamp="22:24:34">NEW VISIT</Visit>
  <Visit User="2" DateStamp="08.06.11" TimeStamp="23:07:48">NEW VISIT/Visit>
</sites>
What I want to do is edit the comment line

Code: Select all

<!--Created: 8/5/2011 7:04:09 PM-->
to the current date when I'll enter a php page.
I know how to load an element and change it but I can't figure out how to load the comment and edit it
this is how I load and edit an element

Code: Select all

 //Load XML and set some config
   $xml = new DOMDocument('1.0', 'utf-8');
   $xml->formatOutput = true;
   $xml->preserveWhiteSpace = false;
   $getName="$file_name.xml";
   $xml->load($getName);

   //Get item Element
   $element = $xml->getElementsByTagName('site')->item(0);

   //Load child elements
   $name = $element->getElementsByTagName('Visit')->item(0);

  //Change values
  $name->nodeValue = 'test';

  //Replace old elements with new
   $element->replaceChild($name, $name);
thanks for the help!!!!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Edit XML comment

Post by Christopher »

I am not sure there is a way. I would recommend using fopen() and the fgets() to read lines from the file. That will be faster. Or you could read the whole file and do string manipulations.
(#10850)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Edit XML comment

Post by McInfo »

For an XML file where the comment immediately follows the doctype, the comment text can be changed with

Code: Select all

$xml->firstChild->nodeValue = 'replacement text';
A more generic solution is this function:

Code: Select all

/**
 * Gets comment child nodes from the given node.
 *
 * @param DOMNode $node
 * @param int $depth
 *     How deep to go in the hierarchy (number of nodes, including the original
 *     node). A number less than 1 means virtually unlimited. Default 0.
 * @return array
 *     An array of DOMComments
 */
function getComments (DOMNode $node, $depth = 0) {
    $children = array();
    if ($node->hasChildNodes()) {
        foreach ($node->childNodes as $child) {
            if ($child->nodeName == '#comment') {
                $children[] = $child;
            } else if ((int)$depth != 1) {
                foreach(getComments($child, $depth - 1) as $comment) {
                    $children[] = $comment;
                }
            }
        }
    }
    return $children;
}

$comments = getComments($xml, 1);
echo $comments[0]->nodeValue;
I don't know why DOMDocument::getElementsByTagName() doesn't find comments when given "#comment" as a name. That is the name returned by DOMNode::nodeName, and there is a precedent for accepting special values: "*".
kingddd
Forum Newbie
Posts: 2
Joined: Tue Aug 09, 2011 12:10 pm

Re: Edit XML comment

Post by kingddd »

Thanks!!!!
It works perfectly!
Post Reply