0

My test.xml like this:

<?xml version="1.0"?>
<!DOCTYPE PLAY SYSTEM "play.dtd">            
<data>
    <CurrentLevel>5</CurrentLevel>
    <BestScoreLV1>1</BestScoreLV1>
    <BestScoreLV2>2</BestScoreLV2>
</data>
<dict/>

My Code here:

std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("text.xml");
tinyxml2::XMLDocument doc;

doc.LoadFile(fullPath.c_str());

tinyxml2::XMLElement* ele =  doc.FirstChildElement("data")->FirstChildElement("BestScoreLV2")->ToElement();
ele->SetAttribute("value", 10);
doc.SaveFile(fullPath.c_str());

const char* title1 =  doc.FirstChildElement("data")->FirstChildElement("BestScoreLV2")->GetText();
int level1  = atoi(title1);
CCLOG("result is: %d",level1);

But value of BestScoreLV2 when output is also 2. How can I change and write data to XML?

mpromonet
  • 11,326
  • 43
  • 62
  • 91

1 Answers1

0

In TinyXML2 text is represented by XMLText class which is child of XMLNode class. XMLNode have methods Value() and SetValue() which have different meanings for different XML nodes. For text nodes Value() read node's text and SetValue() write it. So you need code like this:

tinyxml2::XMLNode* value = doc.FirstChildElement("data")->
    FirstChildElement("BestScoreLV2")->FirstChild();
value->SetValue("10");

The first child of BestScoreLV2 element is XMLText with value 2. You change this value to 10 by calling SetValue(10).