簡體   English   中英

如何從`std :: istream`加載XML文檔?

[英]How to load XML document from `std::istream`?

我想從std::istream加載TinyXml文檔,但它不包含這樣的方法:

/** 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

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

我在這里找到了明確的解決方案:

C ++樣式輸入:

  • 基於std :: istream
  • 操作>>

從流中讀取XML,使其對網絡傳輸很有用。 棘手的部分是知道XML文檔何時完成,因為流中幾乎肯定會有其他數據。 TinyXML將在讀取根元素后假定XML數據已完成。 換句話說,使用多個根元素構造不良的文檔將無法正確讀取。 另請注意,由於STL的實現和TinyXML的限制,operator >>比Parse慢一些。

例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM