如何从std :: istream加载XML文档? [英] How to load XML document from `std::istream`?

查看:136
本文介绍了如何从std :: istream加载XML文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从std::istream加载TinyXml文档,但其中不包含这样的方法:

I'd like to load TinyXml documents from std::istream, but it doesn't contain such method:

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

I see that it contains function that uses FILE, is it possible to convert std::istream to FILE?

推荐答案

我找到了明确的解决方案这里:

I've found clear solution here:

C ++样式输入:

C++ style input:

  • 基于std :: istream
  • 操作员>>

从流中读取XML,使其可用于网络传输. 棘手的部分是知道XML文档何时完成,因为 流中几乎肯定会有其他数据. TinyXML将 假定XML数据在读取根元素后完成.放 另一种方式是,文档结构不完整,且文档不止一个 根元素将无法正确读取.另请注意,运算符>>是 由于STL和 TinyXML的局限性.

Reads XML from a stream, making it useful for network transmission. The tricky part is knowing when the XML document is complete, since there will almost certainly be other data in the stream. TinyXML will assume the XML data is complete after it reads the root element. Put another way, documents that are ill-constructed with more than one root element will not read correctly. Also note that operator>> is somewhat slower than Parse, due to both implementation of the STL and limitations of TinyXML.

示例:

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

这篇关于如何从std :: istream加载XML文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆