Rapidxml-覆盖以前的xml_nodes [英] rapidxml - overwriting previous xml_nodes

查看:82
本文介绍了Rapidxml-覆盖以前的xml_nodes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Rapidxml.我1创建一个要读取的xml文件.如此快速轻松地工作.

I just started using rapidxml. I 1st create an xml file to read from. Worked so fast an easy.

这是我手动创建的内容.

This is what I manual crated.

<?xml version="1.0" encoding="utf-8"?>
<GPS>
    <Path>    
        <Point X="-3684.136" Y="3566.282" Z="285.2893" />
        <Point X="-3681.816" Y="3540.431" Z="283.3658" />
        <Point X="-3687.079" Y="3515.315" Z="282.6284" />
    </Path>
</GPS>

我可以很容易地读到它,没有问题.然后,我想将其写入新文件. 但是问题在于它一直在覆盖以前的xml_nodes.

I could easy read that with no problems. I then wanted to write it to a new file. But the problem is that it keeps overwriting previous xml_nodes.

例如,

<?xml version="1.0" encoding="UTF-8"?>
<GPS>
    <Path>
        <Point X="-3687.08" Y="3515.31" Z="282.628"/>
        <Point X="-3687.08" Y="3515.31" Z="282.628"/>
        <Point X="-3687.08" Y="3515.31" Z="282.628"/>
    </Path>
</GPS>

这是创建该xml文件的代码,

This is the code that creates that xml file,

int Write(pathStruct *toStore)
{
    xml_document<> doc;

    xml_node<>* decl = doc.allocate_node(node_declaration);
    decl->append_attribute(doc.allocate_attribute("version", "1.0"));
    decl->append_attribute(doc.allocate_attribute("encoding", "UTF-8"));
    doc.append_node(decl);  

    xml_node<> *GPS = doc.allocate_node(node_element, "GPS");
    doc.append_node(GPS);

    cout << "Saving GrindPath " << endl;
    xml_node<> *Path = doc.allocate_node(node_element, "Path");
    GPS->append_node(Path);

    for(int i = 0;i<3;i++) //Temp Static
    {
        xml_node<> *Point = doc.allocate_node(node_element, "Point");
        Path->append_node(Point);

        char x[10];
        FloatToCharA(toStore->X[i], x);
        Point->append_attribute(doc.allocate_attribute("X", x));

        char y[10];
        FloatToCharA(toStore->Y[i], y);
        Point->append_attribute(doc.allocate_attribute("Y", y));

        char z[10];
        FloatToCharA(toStore->Z[i], z);
        Point->append_attribute(doc.allocate_attribute("Z", z));

        //GrindPath->append_node(Point);
        //doc.first_node()->append_node(GrindPath);
        //Point = GrindPath->next_sibling();

        cout << "x:" << toStore->X[i] << "    y:" << toStore->Y[i] << "   z:" << toStore->Z[i] << endl;
    }

    cout << "Done! " << endl;
    std::ofstream myfile;
    myfile.open ("./toStore.xml");
    myfile << doc;
    return 0;

};

我的问题是他如何停止覆盖以前的xml_nodes? 我尝试了很多事情,但是每次它仍然覆盖以前的xml_nodes时. 我知道这一定很简单,否则我就错过了全局.

My question his how to I stop it from overwriting previous xml_nodes? I have attempted many thing but every time its still overwrites previous xml_nodes. I know it must be simple or I am missing the big picture.

感谢您的帮助和时间!

推荐答案

我不确定这是否有帮助,但这存在于文档:

I am not sure if this will help but this exists in the documentation:

一个怪癖是节点和属性不拥有其文本 名称和值.这是因为通常它们只存储指向 源文本.因此,在为节点分配新名称或值时, 必须注意确保琴弦的正常使用寿命.这 最简单的方法是从 xml_document内存池.在上面的示例中,这不是必需的, 因为我们只分配字符常量.但是下面的代码 使用memory_pool :: allocate_string()函数分配节点名称 (与文档具有相同的生存期),并将其分配给 一个新节点.

One quirk is that nodes and attributes do not own the text of their names and values. This is because normally they only store pointers to the source text. So, when assigning a new name or value to the node, care must be taken to ensure proper lifetime of the string. The easiest way to achieve it is to allocate the string from the xml_document memory pool. In the above example this is not necessary, because we are only assigning character constants. But the code below uses memory_pool::allocate_string() function to allocate node name (which will have the same lifetime as the document), and assigns it to a new node.

我可以在您的代码中看到,看来您的char数组x,y,z是在循环范围内创建的,因此不满足上述要求.

I can see in your code that it appears that your char arrays x, y, z are created in scope of your loop and as such do not satisfy the requirements above.

这篇关于Rapidxml-覆盖以前的xml_nodes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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