首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用RapidXML写入XML

用RapidXML写入XML
EN

Stack Overflow用户
提问于 2018-12-03 12:32:38
回答 1查看 2.3K关注 0票数 1

我正在尝试用tuple读取我的向量并将其解析为一个xml文件。写入XML文件是可行的,但他只写入最后一个节点的值。所以我一直只得到相同的值。我发现,在for循环之后,他释放了所有现有的节点。也许我对doc.append_node(根)的想法是错误的?!

代码语言:javascript
运行
复制
    #include "XmlHandle.h"
    #include <rapidxml/rapidxml.hpp>
    #include "rapidxml/rapidxml_print.hpp"
    #include <vector>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    #include <sstream>

    using namespace rapidxml;

    XmlHandle::WriteToXml(std::vector<std::tuple<Key, Tone>> vector)
    {
        xml_document<> doc;
        xml_node<>* decl = doc.allocate_node(node_declaration);
        //adding attributes at the top of our xml
        decl->append_attribute(doc.allocate_attribute("version", "1.0"));
        decl->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
        doc.append_node(decl);
        //creating a pointer for the root note "NoteList"
        xml_node<> *root = doc.allocate_node(node_element, "NoteList");

        //iterating through our vector 
        for (std::tuple<Key, Tone> tuple : vector)
        {
            //creating the nodes tuple and key
            xml_node<> *tuple1 = doc.allocate_node(node_element, "Tuple");
            xml_node<> *key = doc.allocate_node(node_element, "Key");
            //getting the char of key saving in d
            char d = std::get<0>(tuple).key;
            //creating a pointer on d
            key->value(&d, 1);
            const char *name = std::string("test").c_str();
            //creating a new node element Tone
            xml_node<> *ToneNode = doc.allocate_node(node_element, "Tone");
            //adding the element to tuple1
            tuple1->append_node(ToneNode);
            //adding the note element Frequency
            xml_node<> *Frequency = doc.allocate_node(node_element, "Frequency");
            float FrequencyFloat = std::get<1>(tuple).frequency;
            std::string FrequencyString = std::to_string(FrequencyFloat);
            //setting the value from frequency
            Frequency->value(FrequencyString.c_str());
            tuple1->append_node(Frequency);
            xml_node<> *Node = doc.allocate_node(node_element, "Node");
            char * NodeValuePinter = (char*)&std::get<1>(tuple).tone;
            char *charPinter = &std::get<1>(tuple).tone;
            Node->value(charPinter, 1);
            ToneNode->append_node(Node);
            tuple1->append_node(key);
            //adding tuple to our root
            root->append_node(tuple1);

        }
        doc.append_node(root);
        std::string xml_as_string;
        rapidxml::print(std::back_inserter(xml_as_string), doc);
        std::ofstream fileStored("file_stored.xml");
        fileStored << xml_as_string;
        fileStored.close();
        doc.clear();
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-06 17:12:07

你的问题可能在这里。

代码语言:javascript
运行
复制
        char d = std::get<0>(tuple).key;
        key->value(&d, 1);

RapidXML存储传递给value()的指针,而不是它指向的字符串。因此,每次在循环中,您的“值”指向相同的东西- 'd‘。

应该使用key->value(doc.allocate_string(&d, 1))将字符数据的副本添加到键中。同样,稍后对node->value也是如此。

Rapidxml is writing wrong characters

绝对是#1 RapidXML抓到你了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53593933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档