当前位置: 技术问答>linux和unix
libxml生成xml文件
来源: 互联网 发布时间:2016-09-12
本文导语: 源代码 #include #include #include int main(int argc, char **argv) { xmlDocPtr doc = NULL; /* document pointer */ xmlNodePtr PARAM_RULE = NULL, VERSION = NULL, node = NULL, node1 = NULL;/* node pointers...
源代码
#include
#include
#include
int main(int argc, char **argv)
{
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr PARAM_RULE = NULL, VERSION = NULL, node = NULL, node1 = NULL;/* node pointers */
doc = xmlNewDoc(BAD_CAST "1.0");
PARAM_RULE = xmlNewNode(NULL, BAD_CAST "PARAM_RULE");
xmlDocSetRootElement(doc, PARAM_RULE);
VERSION = xmlNewChild(PARAM_RULE, NULL, BAD_CAST "VERSION", BAD_CAST"");
xmlNewProp(VERSION, BAD_CAST "major", BAD_CAST "1");
xmlNewProp(VERSION, BAD_CAST "minor", BAD_CAST "0");
xmlAddChild(PARAM_RULE, VERSION);
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();//debug memory for regression tests
return(0);
}
---------------------------------------------------------------
结果是这样的:
以上不是我想要的效果。
-----------------------------------------
我想要的格式:
帮忙修改一下。
#include
#include
#include
int main(int argc, char **argv)
{
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr PARAM_RULE = NULL, VERSION = NULL, node = NULL, node1 = NULL;/* node pointers */
doc = xmlNewDoc(BAD_CAST "1.0");
PARAM_RULE = xmlNewNode(NULL, BAD_CAST "PARAM_RULE");
xmlDocSetRootElement(doc, PARAM_RULE);
VERSION = xmlNewChild(PARAM_RULE, NULL, BAD_CAST "VERSION", BAD_CAST"");
xmlNewProp(VERSION, BAD_CAST "major", BAD_CAST "1");
xmlNewProp(VERSION, BAD_CAST "minor", BAD_CAST "0");
xmlAddChild(PARAM_RULE, VERSION);
xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "UTF-8", 1);
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();//debug memory for regression tests
return(0);
}
---------------------------------------------------------------
结果是这样的:
以上不是我想要的效果。
-----------------------------------------
我想要的格式:
帮忙修改一下。
|
这样行么
xmlNodePtr ptrText = xmlNewText("");
xmlAddChild(PARAM_RULE, ptrText);
xmlNodePtr ptrText = xmlNewText("");
xmlAddChild(PARAM_RULE, ptrText);
|
如果没有text的话,xml就会默认使用/>搞回来!