[xml] Document comments



I am using the <libxml/tree.h> interface to create an XML document.
I need to add comments before the root element; something like this:

<?xml version="1.0"?>
<!--A document comment-->
<root/>

It seems that this can be achieved if I create a comment using
xmlNewDocComment() and then add it to the document with
xmlDocSetRootElement() before I add the actual root element.

I just wanted to verify that this is the correct way to add
document comments.

The following code was used to produce the example above:

#include <libxml/tree.h>

int main(void)
{
  xmlDocPtr document;
  xmlNodePtr root;
  xmlNodePtr comment;
  
  document = xmlNewDoc(BAD_CAST("1.0"));
  root = xmlNewDocNode(document, NULL, BAD_CAST("root"), NULL);
  comment = xmlNewDocComment(document, BAD_CAST("A document comment"));
  xmlDocSetRootElement(document, comment);
  xmlDocSetRootElement(document, root);

  xmlDocDump(stderr, document);
  xmlFreeDoc(document);
  return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]