Cocos2d-x Read XML file
Under the Resource folder, add the Data.xml file
New-"other-" empty-"open"
A new XML file is created,
Data.xml content is as follows
<data>
<p name= "Hello" age= "All"/>
<p name= "World" age= "/>"
<p name= "People" age= "/>"
</data>
To read an XML file, first introduce the header file: #include <tinyxml2/tinyxml2.h>
Add the following code to the bool Helloworld::init ()
1 //Create a document first2Auto doc =Newtinyxml2::xmldocument ();3 //Let Doc parse the string, that is, the name of the file, (using the File tool and then accessing the C-language string)4Doc->parse (Fileutils::getinstance ()->getstringfromfile ("Data.xml"). C_STR ());5 //access to the root node6Auto root = doc->rootelement ();7 //find child objects based on root root node8 //Traverse all sub-objects (, E is not equal to NULL, next)9 for(Auto E = Root->firstchildelement (); e; e = e->nextsiblingelement ()) {Ten OneSTD::stringstr; A - //iterate through all the properties in the current subkey - for(Auto attr = E->firstattribute (); attr; attr = attr->Next ()) { thestr + = Attr->name ();//Get the name -str + =": "; -str + = Attr->value ();//Get Value -str + =", "; + } -Log"%s", Str.c_str ());//output all the information +}
Cocos2d-x Read XML file