【问题标题】:How to load XML document from `std::istream`?如何从 `std::istream` 加载 XML 文档?
【发布时间】:2012-09-09 10:55:32
【问题描述】:

我想从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

【问题讨论】:

  • 不,至少不是以任何标准方式。可以将 FILE 转换为 istream,但这是否足够好?

标签: c++ std tinyxml


【解决方案1】:

我找到了明确的解决方案here

C++ 风格输入:

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

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

例子:

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

【讨论】:

    【解决方案2】:

    istream 加载整个数据,然后使用TiXmlDocument::Parse

    【讨论】:

    • 好主意,但经过一段时间的谷歌搜索后,我找到了更好的解决方案。 :)
    猜你喜欢
    • 2021-04-12
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多