XML is an extensible markup language. In game development, it is often used to save game information, such as the highest score, game level, and other information, and to describe some resources. The first time I used XML was in "Creating Animation with CCAnimation". When using the plist file to load the animation, an XML file is used. The plist file is actually an XML file. The tile map is used in Cocos2d-x (1) and the tile map is used in Cocos2d-x (2). After saving the tile map created by the editor, you will get a file in tmx format. The tmx file is also an XML file.
XML files can also solve the problem of Chinese garbled codes. Chinese encoding garbled codes appear in Cocos2d-X because of the different encoding methods, which are usually used under Windows.
VC is used as an IDE, and VC uses GTK encoding, and Chinese uses UTF-8 encoding. Due to the different encoding methods, direct use of Chinese in Cocos2d-X will cause garbled characters, while xml uses UTF-8 encoding. So using XML can realize the display of Chinese in Cocos2d-X.
I won't talk about the theoretical stuff. If I say too much, I will not understand it. The following will introduce the application of XML in Cocos2d-X through some examples.
Program Example 1: Use CCUserDefault to read the information in XML
Implementation process:
1. Create an XML file
2. Write the highest score into the XML file
3. Read the highest score in the XML file
The creation code of the XML file:
//Save the highest score of the game to the memory
CCUserDefault::sharedUserDefault()->setIntegerForKey(“HighScore”, 7000);
//Write the highest score information of the game to the hard disk
CCUserDefault::sharedUserDefault()->flush();
After successful compilation, an xml file named UserDefault.xml will be generated in the Debug directory.
Code in UserDefault.xml: There is a highest score in UserDefault.xml:
-
7000
Read the highest score in UserDefault.xml
//Read the highest score of the game, return 0 when it is not read
int highScore = CCUserDefault::sharedUserDefault()->getIntegerForKey(“HighScore”, 0);
//Print the highest score
CCLog(“highScore=%d”, highScore);
Results of the:
Program example 2: Use an XML file in plist format to save user information and read user information 1
Implementation process:
Create a new xml file formatted as plist, the contents of the file are as follows
name
Zhang Sanfeng
age
36
Add code to the program
//Create a dictionary class for reading xml files in plist format
CCDictionary* dict = CCDictionary::createWithContentsOfFile(“aaa.plist”);
//Read name information from aaa.plist
const CCString* name = dict->valueForKey(“name”);
//Read age information from aaa.plist
const CCString* age = dict->valueForKey(“age”);
//Print these two information
CCLog(“name is %s, age is %d”, name->getCString(), age->intValue());
Results of the:
22
Program example 3: Use plist format xml file to save user information, and read user information 2
Implementation process:
Create a new xml file formatted as plist, the contents of the file are as follows
name
Zhang Sanfeng
age
36
family
son
name
xxx
age
6
daughter
name
yyy
age
3
Add the following code to the program:
//Create a dictionary class for reading xml files in plist format
CCDictionary* dict = CCDictionary::createWithContentsOfFile(“aaa.plist”);
//Read name information from aaa.plist
const CCString* name = dict->valueForKey(“name”);
//Read age information from aaa.plist
const CCString* age = dict->valueForKey(“age”);
//Print these two information
CCLog(“name is %s, age is %d”, name->getCString(), age->intValue());
//Read family information from aaa.plist
CCObject* oFamily = dict->objectForKey(“family”);
CCDictionary* dictFamily = (CCDictionary*)oFamily;
//Find the information of son in the dictionary
CCDictionary* dictSon = (CCDictionary*)dictFamily->objectForKey(“son”);
//Get the name of son
name = dictSon->valueForKey(“name”);
//Get the age of son
age = dictSon->valueForKey(“age”);
//Print the information of son
CCLog(“the name of son is %s, the age of son is %d”, name->getCString(), age->intValue());
//Find the information of daughter in the dictionary
CCDictionary* dictdaughter = (CCDictionary*)dictFamily->objectForKey(“daughter”);
//Find the name of daughter
name = dictdaughter->valueForKey(“name”);
//Find the age of the daughter
age = dictdaughter->valueForKey(“age”);
//Print the information of daughter
CCLog(“the name of daughter is %s, the age of daughter is %d”, name->getCString(), age->intValue());
Results of the:
Program Example 4: Create an XML file parser
Implementation code:
#include “T40XML_tinyXML.h”
CCScene* T40XML_tinyXML::scene()
{
CCScene* s = CCScene::create();
T40XML_tinyXML* layer = T40XML_tinyXML::create();
s->addChild(layer);
return s;
}
void T40XML_tinyXML::WalkOver(tinyxml2::XMLElement* node)
{
//Get the first child node under the root node
tinyxml2::XMLElement* curNode = node->FirstChildElement();
//Traverse the nodes in xml
while (curNode)
{
if(curNode->FirstChild())
{
CCLog(“node is %s, value is %s”, curNode->Value(), curNode->FirstChild()->Value());
}
else
{
CCLog(“node is %s, value is NULL”, curNode->Value());
}
WalkOver(curNode);
curNode = curNode->NextSiblingElement();
}
}
bool T40XML_tinyXML::init()
{
CCLayer::init();
//Create an xml document
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument;
//Load xml document
doc->LoadFile(“aaa.plist”);
//Print all the content in doc
tinyxml2::XMLElement* rootElement = doc->RootElement();
//Print the content on the root node in aaa.plist
CCLog(“rootElemenet value is %s”, rootElement->Value());
//Print the information of all nodes in aaa.plist
WalkOver(rootElement);
delete doc;
return true;
}