我試圖使用TinyXML從內存中讀取和保存,而不是隻讀取和保存文件到磁盤。TinyXML:將文檔保存到char *或字符串
看來,文檔的解析函數可以加載char *。但是當我完成後,我需要將文檔保存爲char *。有人知道嗎?
編輯:打印&流媒體功能不是我要找的。它們以可見的格式輸出,我需要實際的xml內容。
編輯:印刷很酷。
我試圖使用TinyXML從內存中讀取和保存,而不是隻讀取和保存文件到磁盤。TinyXML:將文檔保存到char *或字符串
看來,文檔的解析函數可以加載char *。但是當我完成後,我需要將文檔保存爲char *。有人知道嗎?
編輯:打印&流媒體功能不是我要找的。它們以可見的格式輸出,我需要實際的xml內容。
編輯:印刷很酷。
我不熟悉TinyXML的,但是從文件看來,通過使用運算< <到C++流(所以你可以使用C++ string streams)或TiXMLPrinter class你可以得到一個STL字符串,而無需使用一個文件。請參閱TinyXML documentation(查找「打印」部分)
不太明白你在說什麼;你的問題不清楚。我猜你想將文件加載到內存中,以便將它傳遞給文檔分析函數。在這種情況下,下面的代碼應該可以工作。
#include <stdio.h>
下面的代碼讀取文件到內存中,並將其存儲在緩衝區
FILE* fd = fopen("filename.xml", "rb"); // Read-only mode
int fsize = fseek(fd, 0, SEEK_END); // Get file size
rewind(fd);
char* buffer = (char*)calloc(fsize + 1, sizeof(char));
fread(buffer, fsize, 1, fd);
fclose(fd);
現在的文件是在變量「緩衝區」,可以傳遞給任何功能需要您提供char *文件的緩衝區。
TinyXml中用於將TiXmlDocument打印到std :: string的簡單優雅的解決方案。
我做這個小例子
// Create a TiXmlDocument
TiXmlDocument *pDoc =new TiXmlDocument("my_doc_name");
// Add some content to the document, you might fill in something else ;-)
TiXmlComment* comment = new TiXmlComment("hello world");
pDoc->LinkEndChild(comment);
// Declare a printer
TiXmlPrinter printer;
// attach it to the document you want to convert in to a std::string
pDoc->Accept(&printer);
// Create a std::string and copy your document data in to the string
std::string str = printer.CStr();
下面是一些示例代碼,我使用,改編自TiXMLPrinter文檔:
TiXmlDocument doc;
// populate document here ...
TiXmlPrinter printer;
printer.SetIndent(" ");
doc.Accept(&printer);
std::string xmltext = printer.CStr();
對不起,我不清楚,編輯。 我已經在使用解析函數,問題是在文件加載後將文件保存回char指針。 – foobar 2008-09-21 07:01:04