2012-09-09 55 views
0

我想從std::istream加載TinyXml的文件,但它不包含這樣的方法:如何從`std :: istream`加載XML文檔?

/** Load a file using the current document value. 
    Returns true if successful. Will delete any existing 
    document data before loading. 
*/ 
bool LoadFile(TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING); 
/// Save a file using the current document value. Returns true if successful. 
bool SaveFile() const; 
/// Load a file using the given filename. Returns true if successful. 
bool LoadFile(const char * filename, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING); 
/// Save a file using the given filename. Returns true if successful. 
bool SaveFile(const char * filename) const; 
/** Load a file using the given FILE*. Returns true if successful. Note that this method 
    doesn't stream - the entire object pointed at by the FILE* 
    will be interpreted as an XML file. TinyXML doesn't stream in XML from the current 
    file location. Streaming may be added in the future. 
*/ 
bool LoadFile(FILE*, TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING); 

我看到它包含使用FILE功能,是有可能std::istream轉換爲FILE

+0

否,至少不以任何標準方式。然而,將FILE轉換爲istream是可行的嗎? – john

回答

1

我發現透明溶液here

C++風格的輸入:

    基礎上的std :: istream的
  • 操作>>

讀取XML從流,使其對網絡傳輸有用。 棘手的部分是知道XML文檔何時完成,因爲 幾乎肯定會有流中的其他數據。 TinyXML將 假定XML數據在讀取根元素後完成。以另一種方式放 ,根元素超過一個 錯誤構建的文檔將無法正確讀取。另請注意,由於STL和TinyXML的限制,運算符>>是 比Parse稍慢。

實施例:

std::istream *in = ResourceManager::getInstance().getResource(resourceName); 
if(in) { 
    TiXmlDocument doc; 
    // load document from resource stream 
    *in >> doc; 
} 
1

istream加載整個數據,然後使用TiXmlDocument::Parse

+0

好主意,但是我花了一些時間在Google上找到了更好的解決方案。 :) – kravemir