Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How do you read an xml file into code?


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   -  Reputation: 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?

Sponsor:

#2 Jacob Jingle   Members   -  Reputation: 223

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   -  Reputation: 15996

Like
0Likes
Like

Posted 06 September 2011 - 11:33 PM

What language are you using?

#4 latent   Members   -  Reputation: 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   -  Reputation: 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   Crossbones+   -  Reputation: 6991

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   -  Reputation: 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   Crossbones+   -  Reputation: 6991

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   -  Reputation: 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   -  Reputation: 320

Like
0Likes
Like

Posted 07 September 2011 - 07:35 AM

libxml?

http://xmlsoft.org/

#11 way2lazy2care   Members   -  Reputation: 782

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   -  Reputation: 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   -  Reputation: 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.



PARTNERS