irrXML tutorial

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

irrXML tutorial

Post by monkeycracks »

Can someone write a C++ tutorial on how to use IrrXML to read and write XML files?
I've tried and failed.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Re: irrXML tutorial

Post by BlindSide »

monkeycracks wrote:Can someone write a C++ tutorial on how to use IrrXML to read and write XML files?
I've tried and failed.
There is already one in the documentation lol. BUT it is flawed, it gives you some wrong functions for initializing the reader I think. Let me find some source code that will help you..

Code: Select all

IXMLReader* xml = device->getFileSystem()->createXMLReader("config.xml");

	int vdriver, xres, yres, bits;
	bool fullscreen;

	while(xml && xml->read())
	{ 
		if (core::stringw("config") == xml->getNodeName())
		{
		vdriver = xml->getAttributeValueAsInt(L"driver");
		xres = xml->getAttributeValueAsInt(L"xres");
		yres = xml->getAttributeValueAsInt(L"yres");
		bits = xml->getAttributeValueAsInt(L"bits");
		fullscreen = xml->getAttributeValueAsInt(L"fullscreen");
		}
	}
Here i create a reader and read some configuration values from an XML file and save them to those variables I created. The file looks like this:

Code: Select all

<?xml version="1.0"?>
<!-- Driver Settings Help: 1 = Direct3D9, 2 = Direct3D8, 3 = OPENGL, 4 = Software, 5 = Software Improved-->
<config driver="3" xres="800" yres="600" bits="32" fullscreen="1" />
DONT forget to include the header "<?xml version="1.0"?>" or Irrlicht will NOT read it.

For the writer it is similar just do this:

Code: Select all

IXMLWriter* xmlr = device->getFileSystem()->createXMLWriter("save.data");
xmlr->writeXMLHeader();
xmlr->writeElement(L"stats",true,health,healthvalue,magic,magicvalue,strength,strengthvalue,smart,smartvalue,somethingelse,somethingelsevalue);	
xmlr->drop();
// (healthvalue is value of health, magicvalue is value of magic, etc etc)
// Here is how i set the "magicvalue" for example:
// wchar_t magicvalue[255];
// swprintf(magicvalue, 255, L"%d",[INTEGER GOES HERE]);
// And do that for all values that you set that are ints, use f% instead of d% for floats. 
// Or replace the whole of "%d" with the words if you just want a string.
// Remember to place all of this BEFORE the writer code...:P
Note you need to have 5 elements there I think. AND DONT FORGET "xmlr->writeXMLHeader();" before writing to any XML file! Else irrlicht can't read it!

Cheers.
Last edited by BlindSide on Sun Jan 28, 2007 8:56 pm, edited 1 time in total.
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

what are you talking about of course can irrliht read xml without the header ;)

but dont forget to drop the writer at the end otherwise the data wont be flushed on the HD

greets,
halan
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Oh yeah, its just thats what it says on the tutorial or somewhere else I read. Woops your right I forgot to drop the writer :oops:
Post Reply