Using .xml files
XML is often used as an API's return value. Cocos2d-x has the TinyXML2 library that can parse an XML file. In this recipe, we will explain how to parse XML files by using this library.
Getting ready
Firstly, you need to create an XML file and save it as test.xml
in the Resources/res
folder in your project. In this case, we will use the following code:
<?xml version="1.0" encoding="UTF-8"?> <root> <key>key text</key> <array> <name>foo</name> <name>bar</name> <name>hoge</name> </array> </root>
To use the TinyXML-2 library, you have to include it and use namespace as follows:
#include "tinyxml2/tinyxml2.h" using namespace tinyxml2;
How to do it...
You can parse an XML file by using the TinyXML2 library. In the following code, we parse test.xml
and log each element in it.
std::string path = util->fullPathForFilename("res/test.xml"); XMLDocument *doc = new XMLDocument...