Forum > C++ > Xml Parsing in cocos2dx

Xml Parsing in cocos2dx

By swapp1990 Posted 2012-05-23 05:59 Comments 17
swapp1990
  • Posts: 15

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"?>
<dict>
<word> Ant </word>
</dict>
@

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.

gramulho
  • Posts: 52

#1 RE: 2012-05-23 21:17

Hello

I use pugixml

http://code.google.com/p/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!

winipcfg
  • Posts: 142

#2 RE: 2012-05-24 01:36

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)

swapp1990
  • Posts: 15

#3 RE: 2012-05-25 11:01

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

swapp1990
  • Posts: 15

#4 RE: 2012-06-18 04:23

@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.

Ozden79
  • Posts: 8

#5 RE: 2012-06-21 13:58

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…

gramulho
  • Posts: 52

#6 RE: 2012-06-21 14:16

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);
Ozden79
  • Posts: 8

#7 RE: 2012-06-21 15:12

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.

gramulho
  • Posts: 52

#8 RE: 2012-06-21 17:11

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.

swapp1990
  • Posts: 15

#9 RE: 2012-06-22 04:00

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);@

Ozden79
  • Posts: 8

#10 RE: 2012-06-22 07:28

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! :) 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…

Ozden79
  • Posts: 8

#11 RE: 2012-06-22 09:22

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!

gramulho
  • Posts: 52

#12 RE: 2012-06-22 12:45

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/

Ozden79
  • Posts: 8

#13 RE: 2012-06-22 12:50

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

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!

gramulho
  • Posts: 52

#14 RE: 2012-06-22 12:55

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.

Ozden79
  • Posts: 8

#15 RE: 2012-06-22 13:01

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

Wish me luck! :D

umni4ek
  • Posts: 54

#16 RE: 2012-08-24 10:18

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

exception handling disabled use -fexceptions to enable

How to solve this problem?

lumendes
  • Posts: 42

#17 RE: 2012-12-11 13:44

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.

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps