Xml Parsing in cocos2dx

Hello, I am a beginner in cocos2dx. Can anyone help me with xml parsing please?
I have a simple xml file, dict.xml like this …
@

<?xml version="1.0" encoding="ISO-8859-1"?> Ant @

I want to load this xml file in project, and display the word “Ant” in my project … what is the best way to do it?
Can anyone give an example basic code with steps clearly explained? I tried searching it elsewhere but no help.
Thank You.

Hello

I use pugixml

It is very straightforward and there is good tutorials on the website. It works very fast and works on every platform my game is running (Win, iOS and Android) along with cocos2dx.

Good luck!

You can use tinyxml (the latest one is tinyxml-2).
You may write yourself or wait for the release (http://www.cocos2d-x.org/issues/616)

Thanks for reply … I used pugixml… Its easy to use :slight_smile:

@joe espindola ….
I used pugixml in my project … It is working perfect when I use it in win32, but the same project when I run in android irt says that the “xml file not found” . I have my xml file in the resources folder. Do I have to make any changes in pugixml for android?? or the filepath?? Please help its urgent … Thank You.

I’m looking for the same answer as well and “CCFileUtils::fullPathFromRelativePath” doesn’t seem to help either! In short, how can we get the path to our assets folder?

Thanks in advance…

Sorry for such a late response! I was so busy i did not had the time to look in my code.

I came up with this issue as well! load_file did not worked under pugixml.

What i did was use cocos2dx CCFileData to get the buffer of the XML and pass it to pugixml using the load_buffer method.

Here is my code:

CCFileData data("myfile.xml", "rb");

unsigned char* pBuffer = data.getBuffer();
unsigned long nSize = data.getSize();

pugi::xml_parse_result result = pugiDocumentLoader_.load_buffer(pBuffer, nSize);

Thanks for the speedy response!

I did test your code in Win32 which works fine but under Android, my game still crashes. Unfortunately I don’t have a debugger attached to it (could barely setup a system that can compile) so I’m not sure what the exact problem is, apart that removing pugixml related codes causes my game to work great :(.

Still, I appreciate your help.

Well, i would strongly advise not coding without a debugger around! Really, you can even debug the c++. Just search on how to do it.

But you if need to see what could have gone wrong there is a couple of things i would try.

First i would try to print the long nSize to see if its greater then 0. If it is less then 0, that might be because CCFileData could not find the file. Are you loading your files from the assets folder ? How are you loading your sprites ?

Try to comment only the load_buffer call… se if your buffer is getting something.

I also used buffer method for loadign data … it worked fine …
you should try using CCFileUtils … See my code …
@
std::string fullPath = CCFileUtils::fullPathFromRelativePath(“dict2.xml”);
unsigned char* pBuffer = NULL;
unsigned long bufferSize = 0;
pBuffer = CCFileUtils::getFileData(fullPath.c_str(), “r”, &bufferSize);
pugi::xml_parse_result result = doc.load_buffer(pBuffer,bufferSize);@

Thank you very much for both of you guys! After a day, I did re-check the project and it seems that the problem is not loading the xml anymore! :slight_smile: After I load some level data, I was creating the levels using Box2D and it seems those Box2D codes are causing the crash.

I did check the loadlibrary section of my activity and there I only see my game code library is loaded, is that normal? I’m sure I created the project with Box2D option. In the libs folder I only see my “libgame.so” file and this is the only thing loaded by loadlibrary. My Android.mk shows some other related libraries as static, such as box2d, and when I searched internet about cocos2d-x + box2d, nobody has any example like this :

LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static box2d_static

Interestingly, cocosdenshion is used as static as well and music + sound works very well within the game? Anybody can point me to some direction on how can I ensure that Box2d is loaded properly?

Thanks…

Ok, Box2D is working fine as well. It seems I need to setup an environment where I can debug the c++ code to see the real cause of the crash. Interestingly, everything works fine under Win32 so I thought this should be enough for the initial development (apart Android specific stuff).

Anyway, thanks for everybody for all their help so far!

Yeah, i strongly advised to setup a proper debugging environment. Coding without knowing the breakpoints is hell! You can debug c++ code on eclipse as well, it helps a lot, its just a bit more extra step.

I used this link and if all steps are done correctly it will work like a charm!

http://blog.plicatibu.com/how-to-debug-cocos2d-x-and-java-code-using-eclipse/

Thanks for the link. I’ve seen it before but seeing all these steps scared me to death! :slight_smile:

I don’t know if you might be interested but, may I offer you a consultancy work to help me setup debugging under macos+eclipse? I would really appreciate if you can help me on that since you successfully did it before!

It looks like a lot of steps, but after you get the first ones done, you will never have to do them again. I can help you without a problem! I would be more happier of you took the time to learn and try it yourself first. If you want to code right you need to learn how things were done. If you skip these steps, they might haunt you down the road! I have been there, and i regretted every second of not toking my time to understand my coding environment.

But if there is anything you need, i can help you! Just post it here!

edit: BTW, i work under Mac Lion 10.7, using Eclipse Indigo Java version. MOTODEV. Cocos2dx 13beta. ndkb7 and latest android sdk. Coded everything under xcode. Just used eclipse to compile and debug my game.

Ok, I’ll give it a try now! If I mess up anything, I’ll ask for your help!

Wish me luck! :smiley:

I have an error while compiling in cygwin pugixml.cpp:

exception handling disabled use -fexceptions to enable

How to solve this problem?

I have an error while compiling in cygwin pugixml.cpp:
exception handling disabled use -fexceptions to enable
How to solve this problem?

pugixml uses C++ exception handling.
Add LOCAL_CPPFLAGS += -fexceptions to your Android.mk.