2013-04-20 47 views
0

我的test.xml這樣的:如何在IOS寫入數據TINYXML2

<?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?

回答

0

在TinyXML2文本中,XMLText類是XMLNode類的子類。 XMLNode具有方法Value()SetValue(),它們對於不同的XML節點具有不同的含義。 對於文本節點Value()讀取節點的文本並將其寫入SetValue()。 因此,你需要這樣的代碼:

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

BestScoreLV2元素的第一個孩子是XMLText與價值2。您可以撥打SetValue(10)將此值更改爲10