在微小的Xml元素中更新数据 [英] Updating Data in tiny Xml element

查看:96
本文介绍了在微小的Xml元素中更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是:是否可以更改xml元素中的数据?

My question is: is it possible to change the data in an xml element?

我要做的是根据所按下的按钮更改元素中的数据.我目前可以读取和写入xml文件,但我想将其更改为,第一次写一个新元素,然后编辑该元素,因为它当前每次都在不断写入一个新元素.

What i want to do is change the data in the element depending on what button is pressed. I currently have it reading and writing to the xml file working but i want to change it to, write a new element first time round and then after that edit the element as it currently just keeps writing a new element each time.

这是我当前编写新元素的代码

This is my current code for writing the new element

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%确定所需的行为.这是基于您的问题代码示例的代码示例:

I'm not 100% sure what the desired behavior you want. Here is a code sample based off of your question code sample:

#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文件:

Given a XML file that looks like this:

<Assets>
<ImagePaths>
</ImagePaths>
</Assets>

运行后,它看起来像这样:

After running it would look like this:

<Assets>
<ImagePaths>
    <!--Selected Player-->
    <path name="selected_player">New Text</path>
</ImagePaths>
</Assets>

如果再次运行该程序,您将仅获得带有选择字符串包含的文本的单路径节点.

And if you run the program again you get just the single path node with the text that your choice string contains.

希望有帮助!

这篇关于在微小的Xml元素中更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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