2016-07-31 34 views
0

我試圖用libxml2保存utf8編碼的html,它工作正常,但非ascii字符保存爲Г。代碼用於保存文件:libxml2 htmlSaveFileEnc將utf8字符保存爲Г

htmlSaveFileEnc("modified.html", docPtr, "utf8"); 

我怎樣才能避免這種情況,並保存爲

Г

utf8字符?

回答

1

作爲解決方法,使用htmlDocContentDumpOutput()函數。將文檔內容轉儲到字符緩衝區並將緩衝區寫入文件。

//htmlSaveFileEnc("modified.html", docPtr, "utf8"); 
xmlOutputBufferPtr out = xmlAllocOutputBuffer(NULL); 
if (out) { 
    htmlDocContentDumpOutput(out, docPtr, "utf8"); 
    const xmlChar *buffer = xmlBufferContent((xmlBuffer *) out->buffer);  
    // write buffer to file 
    FILE *file = fopen("modified.html", "w"); 
    fputs((char *) buffer, file); 
    fclose(file); 

    xmlOutputBufferClose(out); 
} 
+0

非常感謝,幫助。 – crashtua