我的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);但是当输出也是2时,BestScoreLV2的值也是2。如何更改和写入数据呢?
发布于 2013-04-20 19:56:00
在TinyXML2中,文本由XMLText类表示,该类是XMLNode类的子类。XMLNode有Value()和SetValue()方法,它们对不同的节点有不同的含义。对于文本节点,Value()读取节点的文本,然后SetValue()写入。所以你需要这样的代码:
tinyxml2::XMLNode* value = doc.FirstChildElement("data")->
FirstChildElement("BestScoreLV2")->FirstChild();
value->SetValue("10");BestScoreLV2元素的第一个子元素是值为2的XMLText。您可以通过调用SetValue(10)将此值更改为10。
https://stackoverflow.com/questions/16119163
复制相似问题