Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4140985
  • 博文数量: 447
  • 博客积分: 1241
  • 博客等级: 中尉
  • 技术积分: 5786
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 06:48
个人简介

读好书,交益友

文章分类

全部博文(447)

文章存档

2023年(6)

2022年(29)

2021年(49)

2020年(16)

2019年(15)

2018年(23)

2017年(67)

2016年(42)

2015年(51)

2014年(57)

2013年(52)

2012年(35)

2011年(5)

分类: C/C++

2014-02-25 15:06:27

pugixml使用中发现,没有直接的方法把数据输出到std::string.
看了以下文档,原来也很简单。

定义结构体
struct xml_string_writer : pugi::xml_writer
  {
    std::string result;
    virtual void write(const void* data, size_t size)
      {
        result += std::string(static_cast(data), size);
      }
  };
输出内容
int genshowxml(string &xmlstring,string rootkey,string key, vector &v)
{
    pugi::xml_document doc;
    pugi::xml_node decl = doc.prepend_child(pugi::node_declaration);
    decl.append_attribute("version") = "1.0";
    decl.append_attribute("encoding") = "UTF-8";
    decl.append_attribute("standalone") = "no";
    //[code_modify_add
              // add node with some name
    pugi::xml_node root = doc.append_child("root");
    pugi::xml_node node = root.append_child(rootkey.c_str());
    // add description node with text child
    vector::iterator it;
    for(it=v.begin();it!=v.end();++it)
    {
      pugi::xml_node descr = node.append_child(key.c_str());
      string tmp = *it;
      descr.append_child(pugi::node_pcdata).set_value(tmp.c_str());
    }

    xml_string_writer writer;

    doc.save(writer);
    xmlstring = writer.result;
    //]
    return 0;

}
目前不处理格式和编码问题。
阅读(7673) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~