如何使用libxml2将XML保存到文件(filename.xml)? [英] how to save XML to a file (filename.xml) using libxml2 ?

查看:430
本文介绍了如何使用libxml2将XML保存到文件(filename.xml)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用" --with-minimum -with--schemas "开关(配置选项)配置了libxml2. 我正在使用"xmlNewNode","xmlAddChild"等函数来生成XML文件. 现在,我想将XML保存到文件中,但是我没有找到任何执行此操作的功能.

I've configured libxml2 with " --with-minimum -with--schemas " switches (configuration options). I am using "xmlNewNode" "xmlAddChild" etc. functions to generate the XML file. Now, i want to save the XML to a file, but i didn't find any function to do that.

当然,"libxml2完整版"中有很多功能(例如xmlSaveFile()等.)但我说过我已经使用--minimum配置选项(内存约束b/c)对其进行了配置

Of-course, there are plenty of functions (like xmlSaveFile() etc..) in "libxml2 full version" BUT as i said i've configured it with --minimum configuration option (b/c of memory constraints).

有人帮助吗? 谢谢 !

Any one with help ? Thanks !

推荐答案

xmlDocDumpMemory将使您将whoe xml文档作为xmlChar *获取.
xmlChar *xmlbuff;
int buffersize;
xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);

xmlDocDumpMemory will let you get the whoe xml doc as a xmlChar*
xmlChar *xmlbuff;
int buffersize;
xmlDocDumpFormatMemory(doc, &xmlbuff, &buffersize, 1);

之后,您可以使用将其另存为文件
FILE *fp;
fp = fopen(file, "w+");
if(!fp){
printf("Could not open file for writing\n");
return;
}
fprintf(fp, buf);
fclose(fp);

after which you can save it as a file using
FILE *fp;
fp = fopen(file, "w+");
if(!fp){
printf("Could not open file for writing\n");
return;
}
fprintf(fp, buf);
fclose(fp);

这篇关于如何使用libxml2将XML保存到文件(filename.xml)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆