rapidxml – 覆盖以前的xml_nodes

我刚开始使用rapidxml.我首先创建一个xml文件来读取.工作得这么快就很容易了.

这是我手动创作的.

<?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.

例如,

<?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文件代码,

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.
我知道这一定很简单,或者我错过了大局.

谢谢你的帮助和时间!

我不确定这是否会有所帮助,但这在 the documentation中存在:

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似乎是在你的循环范围内创建的,因此不满足上面的要求.

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念