How do you read an xml file into code?

Started by
11 comments, last by NetGnome 12 years, 7 months ago
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?
Advertisement

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.
What language are you using?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


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.

What language are you using?


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

[quote name='ApochPiQ' timestamp='1315373589' post='4858492']
What language are you using?


I'm using c++, and I'd like to keep it platform independent.
[/quote]
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.
[size=2][ 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 ]
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.

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.
[size=2][ 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 ]
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.
libxml?

http://xmlsoft.org/

This topic is closed to new replies.

Advertisement