COCOS2DX 3.1.1 Using Tinyxml2.h to interpret XML (c + +)

Source: Internet
Author: User
Tags gettext lua

COCOS2DX 3.1.1 How to interpret XML with Tinyxml2.h? (c + +)

COCOS2DX already has its own TINYXML2 for XML, and since the beginning of the 2.x version, it is no longer necessary to download it.

However, tinyxm2 about the 3.x engine documentation is relatively small, hereby to contribute one!


First, add the header file:

#include "Cocos-ext.h"

#include "Tinyxml2/tinyxml2.h"

using namespace TINYXML2;

Using namespace std;



Example 1:

Text.xml file contents are as follows

<?xml version= "1.0"?>

<Hello>World</Hello>

XML interpretation:

 string File_ Path = Fileutils::getinstance ()->fullpathforfilename<span style= "FONT-FAMILY:MENLO; font-size:11px; " > (</span><span style= "Color:rgb (209, N.); font-family:menlo; font-size:11px;" > "Testset.xml" </span><span style= "FONT-FAMILY:MENLO; font-size:11px; " >);//</span><span style= "FONT-FAMILY:MENLO; font-size:11px; " > If the new is </span><span style= "FONT-FAMILY:MENLO; font-size:11px; " >lua project needs to write </span><span style= "FONT-FAMILY:HANNOTATESC-W5;" > ("Res/text.xml"); </span><span style= "FONT-FAMILY:MENLO; font-size:11px; "    ></span> log ("External file path =%s", file_path.c_str ());    XMLDocument Doc; Load file doc.    LoadFile (File_path.c_str ()); Const char* content= Doc.    Firstchildelement ("Hello")->gettext (); Log ("hello,%s", content); 


Output result Hello,world


Example 2:

Hello.xml File Contents

<?xml version="1.0"?>

<scene Name="Depth">

<node type="Camera">

<eye>0</eye>

<front>0 0-1</front>

<refUp>0 1 0</refUp>

<fov></fov>

</node>

<node type="Sphere">

<center>0 10-10</center>

<radius></radius>

</node>

<node type="Plane">

<direction>0 10-10</direction>

<distance></distance>

</node>

</scene>


XML parsing:

    String file_path = Fileutils::getinstance ()->fullpathforfilename ("Hello.xml"); <span style= "Font-family:menlo ; font-size:11px; " >//if a new LUA project is required to write </span><span style= "FONT-FAMILY:MENLO;" > ("Res/hello.xml"); </span><span style= "FONT-FAMILY:MENLO; font-size:11px; "    ></span> log ("External file path =%s", file_path.c_str ());    XMLDocument document; Document.    LoadFile (File_path.c_str ()); XMLElement *scene=document.    RootElement ();    XMLElement *surface=scene->firstchildelement ("node");        while (surface) {XMLElement *surfacechild=surface->firstchildelement ();        const char* Content;        Const XmlAttribute *attributeofsurface = Surface->firstattribute ();        Log ("%s:%s", Attributeofsurface->name (), Attributeofsurface->value ());            while (Surfacechild) {content=surfacechild->gettext ();            Surfacechild=surfacechild->nextsiblingelement ();        Log ("%s", content); }       Surface=surface->nextsiblingelement (); }


Output Result:

Cocos2d:type:camera

cocos2d:0 10 10

cocos2d:0 0-1

cocos2d:0 1 0

Cocos2d:90

Cocos2d:type:Sphere

cocos2d:0 10-10

Cocos2d:10

Cocos2d:type:Plane

cocos2d:0 10-10

Cocos2d:10


Resources:

http://blog.csdn.net/educast/article/details/12908455

COCOS2DX 3.1.1 Using Tinyxml2.h to interpret XML (c + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.