Skip to main content
gamedev.net
🔒 Locked

TinyXML

Started by Arazux 4 replies 1.9k views
Original Post
Arazux
Arazux
I've only found one tutorial for this so far and it's linked from the official homepage and I'd really like some help with what seems to me should be a really simple task. I can easily handle the output and building of XML files, I have a problem with the loading tho. Imagine I have an XML file like this: <Character> <Name>Arazux</Name> <Level>19</Level> <Position> <X>4</X> <Y>2</Y> </Position> </Character> Could someone give me some code to help me load this into C++? I keep getting loads of error with overflows and whatnot, so far I've only managed to with the Value() function get the string "Character" which I don't even want/need. Edited: I am using TinyXML by the way, is there any other XML parser which is easier to understand? Thankful for help and tips.
stormwarestudios
stormwarestudios
FTFM:
Quote:

Handles

Where browsing an XML document in a robust way, it is important to check for null returns from method calls. An error safe implementation can generate a lot of code like:

TiXmlElement* root = document.FirstChildElement( "Document" );
if ( root )
{
TiXmlElement* element = root->FirstChildElement( "Element" );
if ( element )
{
TiXmlElement* child = element->FirstChildElement( "Child" );
if ( child )
{
TiXmlElement* child2 = child->NextSiblingElement( "Child" );
if ( child2 )
{
// Finally do something useful.

Handles have been introduced to clean this up. Using the TiXmlHandle class, the previous code reduces to:

TiXmlHandle docHandle( &document );
TiXmlElement* child2 = docHandle.FirstChild( "Document" ).FirstChild( "Element" ).Child( "Child", 1 ).Element();
if ( child2 )
{
// do something useful

Which is much easier to deal with. See TiXmlHandle for more information.


Are you able to run the final bit of code? e.g.
TiXmlHandle docHandle( &document );std::string szName = docHandle.FirstChild( "Character" ).FirstChild( "Name" ).GetText();

??

On a side note, I would recommend sticking it out with TinyXML; it's a great library, and probably one of the simpler ones you'll find out there (short of reinventing the wheel writing an XML parser yourself).
Arazux
Arazux
1>c:\users\mike\documents\visual studio 2008\projects\xmltest\xmltest\main.cpp(162) : error C2039: 'GetText' : is not a member of 'TiXmlHandle'

When trying to compile the code you gave me.

Also seems kind of annoying to have to write "Character" and "Name" everytime, is this neccessary if I just want to get for example position coordinates?
Stereo
Stereo
I like XMLParser. 1 header, 1 source file, easy to use and fast.
Arazux
Arazux
I've got most of it working now. Just need help with a really quick problem.

If for example I want to read in a player.position.x value from an XML file looking like this:

<Position>
<X>4</X>
</Position>

I know how to get the const char * 4, but I don't know how to typecast it to an int, I've tried a few things but they didnt work. Basically, any way I can make it return an int if I know that the function is supposed to return one? Or is there any way I can make the const char *text which gets the value 4 into an int at a later point?
King of Men
King of Men
You want the 'atoi' function, like so:

  char* numberFourInText = getValueFromXml();  int actualNumberFour = atoi(numFourInText);
To win one hundred victories in one hundred battles is not the acme of skill. To subdue the enemy without fighting is the acme of skill.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.