我刚开始使用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。我知道这一定很简单,否则我错过了大局。
感谢您的帮助和时间!