Reputation: 2241
I am using libxml++-2.6 in C to create a really simple XML doc.
What is the technique for adding:
.
root_node = xmlNewNode( NULL, BAD_CAST "root" );
xmlDocSetRootElement( doc, root_node );
dtd = xmlCreateIntSubset(doc, BAD_CAST "root", NULL, BAD_CAST "root.dtd");
// neither of these seem to work
xmlNewComment( BAD_CAST "My Company, LLC" );
xmlNewDocComment ( doc, BAD_CAST "My Company, LLC" );
Upvotes: 2
Views: 1306
Reputation: 2241
Before this got moved here I had posted my own answer in StackOverflow. Sorry to post it as an answer, but you can't do formatted inopput in comments. :-)
This has worked for me;
root_node = xmlNewNode( NULL, BAD_CAST "root" );
xmlDocSetRootElement( doc, root_node );
//==========================================================================
// Comment block ABOVE the root node
//==========================================================================
cur_node = xmlAddPrevSibling( root_node, xmlNewComment( BAD_CAST copyright ));
xmlAddNextSibling( cur_node, xmlNewComment( BAD_CAST generated ));
cur_node = x
mlNewChild( root_node, NULL, BAD_CAST "Model" , BAD_CAST "FRED" );
//==========================================================================
// Comment block inside the root node but ABOVE the Model node
//==========================================================================
cur_node = xmlAddPrevSibling( root_node, xmlNewComment( BAD_CAST modinfo ));
xmlAddNextSibling( cur_node, xmlNewComment( BAD_CAST more_modinfo ));
Notice that as you add a node, you put the comment before it then put additional linesof comment below that comment but still before the new doc/child element.
Also notice, these comments can be outside the root node and not inside a visible node thatis a sibling of root.
e.g.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE TEST SYSTEM "testsystem.dtd">
<!--Copyright (c) 2012 SuperTechnology, LLC.-->
<!--Generated Jan 20 2012 16:15:11-->
<root_node>
Upvotes: 0
Reputation: 4829
#include <stdio.h>
#include <libxml/tree.h>
int
main()
{
xmlDocPtr foo = xmlNewDoc("1.0");
xmlNodePtr com = xmlNewDocComment(foo, "bla bla");
xmlNodePtr ins = xmlNewDocComment(foo, "more bla");
xmlNodePtr roo = xmlNewDocNode(foo, NULL, "test", NULL);
xmlDocSetRootElement(foo, com);
xmlAddSibling(foo, roo);
xmlAddChild(roo, ins);
xmlDocDump(stdout, foo);
return 0;
}
which results in:
<?xml version="1.0"?>
<!--bla bla-->
<test><!--more bla--></test>
Upvotes: 2