how to save XML to a file (filename.xml) using libxml2 ?

我是研究僧i 提交于 2019-12-12 04:49:52

问题


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.

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 !


回答1:


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

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);



来源:https://stackoverflow.com/questions/9340268/how-to-save-xml-to-a-file-filename-xml-using-libxml2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!