0

私のtest.xmlは次のようになります:

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

ここに私のコード:

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);

しかし、出力時の BestScoreLV2 の値も 2 です。どうすればデータを変更して XML に書き込むことができますか?

4

1 に答える 1

0

XMLTextTinyXML2 では、テキストはクラスの子であるクラスによって表されますXMLNodeXMLNodeメソッドがValue()あり、SetValue()XML ノードごとに異なる意味を持ちます。テキスト ノードValue()の場合、ノードのテキストを読み取り、それをSetValue()書き込みます。したがって、次のようなコードが必要です。

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

BestScoreLV2elementの最初の子はXMLTextvalue2です。10を呼び出して、この値を に変更しますSetValue(10)

于 2013-04-20T11:55:59.937 に答える