正在更新微小Xml元素中的数据
Updating Data in tiny Xml element
我的问题是:是否可以更改xml元素中的数据?
我想做的是根据按下的按钮来更改元素中的数据。我目前正在读取和写入xml文件,但我想将其更改为,第一次写入一个新元素,然后编辑该元素,因为它目前每次只写一个新元素。
这是我当前编写新元素的代码
if (doc.LoadFile(XMLDOC) == tinyxml2::XML_SUCCESS){
//Get Root Node
tinyxml2::XMLElement* rootNode = doc.FirstChildElement();//Assets
//Get Next Node
tinyxml2::XMLElement* childNode = rootNode->FirstChildElement();//imagePaths
//Temp Element
tinyxml2::XMLElement* temp = nullptr;
tinyxml2::XMLElement* temp2 = childNode->FirstChildElement();//path
while (temp2 != nullptr){
temp = temp2;
temp2 = temp2->NextSiblingElement("path");
}
if (temp != nullptr){
//write the text
tinyxml2::XMLComment* newComment = doc.NewComment("Selected Player");
tinyxml2::XMLElement* newElement = doc.NewElement("path");
//get text passed in
newElement->SetText(choice.c_str());
newElement->SetAttribute("name", "selected_player");
childNode->InsertAfterChild(temp, newComment);
childNode->InsertAfterChild(newComment, newElement);
}
//doc.Print();
doc.SaveFile(XMLDOC);
}
else{
std::cout << "Could Not Load XML Document : %s" << XMLDOC << std::endl;
}
}
感谢您在高级方面的帮助
我不能100%确定您想要什么样的行为。以下是基于您的问题代码示例的代码示例:
#include "tinyxml2.h"
#include <iostream>
#include <string>
#define XMLDOC "test.xml"
std::string choice = "New Text";
int main()
{
tinyxml2::XMLDocument doc;
if (doc.LoadFile(XMLDOC) == tinyxml2::XML_SUCCESS){
//Get Root Node
tinyxml2::XMLElement* rootNode = doc.FirstChildElement();//Assets
//Get Next Node
tinyxml2::XMLElement* childNode = rootNode->FirstChildElement();//imagePaths
//Path Node
tinyxml2::XMLElement* pathNode = childNode->FirstChildElement();//path
if (pathNode == nullptr){
//write the text
tinyxml2::XMLComment* newComment = doc.NewComment("Selected Player");
tinyxml2::XMLElement* newElement = doc.NewElement("path");
newElement->SetAttribute("name", "selected_player");
newElement->SetText(choice.c_str());
childNode->InsertFirstChild(newComment);
childNode->InsertAfterChild(newComment, newElement);
}
else{
pathNode->SetText(choice.c_str());
}
doc.SaveFile(XMLDOC);
}
else{
std::cout << "Could Not Load XML Document : " << XMLDOC << std::endl;
}
}
给定一个如下所示的XML文件:
<Assets>
<ImagePaths>
</ImagePaths>
</Assets>
运行后,它看起来像这样:
<Assets>
<ImagePaths>
<!--Selected Player-->
<path name="selected_player">New Text</path>
</ImagePaths>
</Assets>
如果你再次运行该程序,你只会得到一个路径节点,其中包含你的选择字符串所包含的文本。
希望能有所帮助!
相关文章:
- 为什么复制而不是移动数据元素?
- 设计将引用元素移动到开头的数据结构.C++
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 如何为结构的数据元素赋值
- C++ Expat 仅打印元素的第一个字母和标签中的数据
- C++ STL 数据结构常时按索引推送/弹出/随机访问,并具有指向元素的可靠指针
- 从自定义数据类型向量中删除重复元素
- C++ - 按自定义数据类型向量的值删除元素
- Getter 函数,用于在 2d 数组是类的数据成员时检索 2d 数组元素
- unordered_set是否适合存储矢量<int>元素的数据结构?如果是这样,我将如何实现哈希函数?
- 使用 dectlype 推断模板元素类型中的数据类型是否正确?
- 用于查找元素的数据结构
- C++ 数据结构队列:使用 for 循环查找队列中最大的元素
- 尝试读取数据文件,存储在数组中并打印所有元素,但它不起作用
- 固定大小的容器,其中元素被排序,并可以为C 中的数据提供原始指针
- 从右到左计算当前索引中的数据结构中的元素数,并打印每个元素的计数
- 如何编写循环以通过列表迭代并打印列表中每个元素的数据的数据
- 向量元素数据损坏了Find()操作
- 在C++继承中,派生类如何禁用基类的某些元素(数据或函数)