我在C中使用libxml ++ - 2.6来创建一个非常简单的XML文档。
添加的技术是什么:
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" );
答案 0 :(得分:2)
#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;
}
导致:
<?xml version="1.0"?>
<!--bla bla-->
<test><!--more bla--></test>
答案 1 :(得分:0)
在此之前,我已经在StackOverflow中发布了自己的答案。很抱歉发布它作为答案,但你不能在评论中格式化inopput。 : - )
这对我有用;
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 ));
请注意,在添加节点时,您将注释放在它之前,然后在该注释下面添加额外的注释行,但仍然在新的doc /子元素之前。
另请注意,这些注释可以在根节点之外,而不是在作为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>