Jump to content

View more

Image of the Day

Working on an auto spawn system. #gamedev #indiedev #screenshotsaturday https://t.co/Mm2kfekz7b
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

How do you read an xml file into code?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
12 replies to this topic

#1 TheAsterite   Members   

203
Like
0Likes
Like

Posted 06 September 2011 - 10:25 PM

Hello,

I'm trying to read in an xml config file with a little information such as screen width, height, and stuff like that, but I'm having a hard time of knowing where to start. I installed an xml parser called Xerces, but there's really no documentation for beginners of the api, and all I'm doing is getting frustrated.

So my question is what's an easy xml parser to pick up with some good tutorials and documentation?

#2 Jacob Jingle   Members   

226
Like
1Likes
Like

Posted 06 September 2011 - 10:43 PM

Hello,

I'm trying to read in an xml config file with a little information such as screen width, height, and stuff like that, but I'm having a hard time of knowing where to start. I installed an xml parser called Xerces, but there's really no documentation for beginners of the api, and all I'm doing is getting frustrated.

So my question is what's an easy xml parser to pick up with some good tutorials and documentation?

If your aiming for windows I would use MSXML. Good examples and good documentation.

#3 ApochPiQ   Moderators   

22536
Like
0Likes
Like

Posted 06 September 2011 - 11:33 PM

What language are you using?
Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

#4 latent   Members   

139
Like
0Likes
Like

Posted 06 September 2011 - 11:33 PM

So my question is what's an easy xml parser to pick up with some good tutorials and documentation?


I just started using TinyXML as I rather like the license, it's tiny, and it works alongside my existing code. There are examples/tutorials on that doc page.

#5 TheAsterite   Members   

203
Like
0Likes
Like

Posted 07 September 2011 - 12:14 AM

What language are you using?


I'm using c++, and I'd like to keep it platform independent.

#6 Cornstalks   Members   

7030
Like
0Likes
Like

Posted 07 September 2011 - 12:22 AM

What language are you using?


I'm using c++, and I'd like to keep it platform independent.

I've really enjoyed using pugixml. I particularly like the fact that it has XPath support. Of course, your needs may vary, and pugixml may not be for you, but I would heartily recommend it. It's just 3 files (2 if you take out the configuration header), so I've easily just added it to my project and am using it successfully.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#7 TheAsterite   Members   

203
Like
0Likes
Like

Posted 07 September 2011 - 12:31 AM

Thanks for all the suggestions, I'll look into some of these tomorrow.


What I'm trying to do is really basic, it's just to load a simple config file for a game engine, such as height, width, fov, stuff like that.

#8 Cornstalks   Members   

7030
Like
0Likes
Like

Posted 07 September 2011 - 12:44 AM

Thanks for all the suggestions, I'll look into some of these tomorrow.


What I'm trying to do is really basic, it's just to load a simple config file for a game engine, such as height, width, fov, stuff like that.

Just a little code example of what my code is kind of like with pugixml and XPath:

#include <iostream>


#include "pugixml.hpp"

int main()
{
  try
  {
    pugi::xml_document doc;
    pugi::xml_parse_result result = doc.load_file("filename.xml");
    
    // only if the loading was successful
    if (result)
    {
      int width = 0;
      int height = 0;
      std::string title;
      
      pugi::xpath_node node = doc.select_single_node("/Window/Width/@Value");
      if (node)
      {
        width = node.attribute().as_int();
      }
      
      node = doc.select_single_node("/Window/Height/@Value");
      if (node)
      {
        height = node.attribute().as_int();
      }
      
      node = doc.select_single_node("/Window/Title/@Value");
      if (node)
      {
        title = node.attribute().value();
      }
    }
    else
    {
      std::cout << "Error: " << result.description() << std::endl;
      std::cout << "Error offset: " << result.offset << std::endl;
    }
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << std::endl;
  }
}

It's fairly straightforward and simple. Just thought I'd give a little demo. Of course, like I said, pugixml may not meet your needs the best though (but it sure meets mine!), so be sure to look at the others and just do a little comparison.

Disclaimer: The code above hasn't actually been tested.
[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#9 pulpfist   Members   

528
Like
0Likes
Like

Posted 07 September 2011 - 07:30 AM

I have been using TinyXML and RapidXml myself. They both worked fine for small tasks.
I don't think they support XPath though, and they don't support the DTD language.

#10 undead   Members   

368
Like
0Likes
Like

Posted 07 September 2011 - 07:35 AM

libxml?

http://xmlsoft.org/

#11 way2lazy2care   Members   

786
Like
0Likes
Like

Posted 07 September 2011 - 09:06 AM

Thanks for all the suggestions, I'll look into some of these tomorrow.

What I'm trying to do is really basic, it's just to load a simple config file for a game engine, such as height, width, fov, stuff like that.


Do you want your XML file to be read at run time or compile time?

#12 sungupta   Members   

103
Like
0Likes
Like

Posted 12 September 2011 - 06:50 AM

I've used Liquid XML Editor in the past which has a pretty decent xml parsing feature plus all the other usual fetaures like data binding, validation etc

#13 Net Gnome   Members   

773
Like
0Likes
Like

Posted 12 September 2011 - 01:11 PM

Just an FYI:

Keywords you're looking for in your solution are Marshalling / Unmarshalling & Serialization / Deserialization which is a form of XML Data Binding. From what i'm understanding you want to deserialize / unmarshal the XML into an object reference then access it from there, rather than performing manual searches. A google search with these keyword terms will give you exactly what you're looking for :)




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.