C++ 使用RapidXML将xml文件内容转换为字符串
我正在使用RapidXML修改现有的XML文档。这是我的代码:C++ 使用RapidXML将xml文件内容转换为字符串,c++,rapidxml,C++,Rapidxml,我正在使用RapidXML修改现有的XML文档。这是我的代码: file<> xmlFile( launchFullPath.c_str() ); xml_document<> doc; doc.parse<0>(xmlFile.data()); // create a node called basedir xml_node<> *basedir = doc.allocate_node(node_elem
file<> xmlFile( launchFullPath.c_str() );
xml_document<> doc;
doc.parse<0>(xmlFile.data());
// create a node called basedir
xml_node<> *basedir = doc.allocate_node(node_element, "basedir");
// append basedir to the launch
xml_node<> *node = doc.first_node("launch");
node->append_node(basedir);
// append attribute to basedir
xml_attribute<> *attr = doc.allocate_attribute("path", SUMOfullDirectory.c_str());
basedir->append_attribute(attr);
文件xmlFile(launchFullPath.c_str());
xml_文档文档;
parse(xmlFile.data());
//创建一个名为basedir的节点
xml_node*basedir=doc.allocate_node(节点_元素,“basedir”);
//将basedir附加到启动
xml_node*node=doc.first_node(“启动”);
节点->附加_节点(basedir);
//将属性附加到basedir
xml_attribute*attr=doc.allocate_属性(“path”,SUMOfullDirectory.c_str());
basedir->append_属性(attr);
最终,XML的内容将是:
<launch>
<copy file="hello.net.xml"/>
<copy file="hello.rou.xml"/>
<copy file="hello.loopDetector.xml"/>
<copy file="hello.obstacles.xml"/>
<copy file="hello.sumo.cfg" type="config"/>
<basedir path="/home/mani/Desktop/VENTOS/sumo/Incident_Detection"/>
<seed value="0"/>
</launch>
最后,我想将XML文档的内容转换为字符串。正如您所看到的(从EclipseIDE中的调试模式),XML文件的内容没有正确地存储到st变量中。我也使用了RapidXML::print方法,结果是一样的。这里有什么问题
请注意,Rapidxml是一个“原位”解析器,因此我认为它可以直接在示例中的文件缓冲区上工作。(是您的文件
类提升还是其他什么?)一旦关闭该文件,您的文档
将成为垃圾。您是否有完整的示例来复制该问题?