• Advertisement
Sign in to follow this  

How do you read an xml file into code?

This topic is 2322 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
[quote name='TheAsterite' timestamp='1315369535' post='4858477']
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?
[/quote]
If your aiming for windows I would use [url="http://msdn.microsoft.com/en-us/data/bb190600"]MSXML[/url]. Good examples and good documentation.

Share this post


Link to post
Share on other sites
[quote name='TheAsterite' timestamp='1315369535' post='4858477']
So my question is what's an easy xml parser to pick up with some good tutorials and documentation?
[/quote]

I just started using [url="http://www.grinninglizard.com/tinyxmldocs/index.html"]TinyXML[/url] as I rather like the license, it's tiny, and it works alongside my existing code. There are examples/tutorials on that doc page.

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1315373589' post='4858492']
What language are you using?
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='TheAsterite' timestamp='1315376054' post='4858498']
[quote name='ApochPiQ' timestamp='1315373589' post='4858492']
What language are you using?
[/quote]

I'm using c++, and I'd like to keep it platform independent.
[/quote]
I've really enjoyed using [url="http://pugixml.org/"]pugixml[/url]. I particularly like the fact that it has [url="http://www.w3schools.com/xpath/"]XPath[/url] 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='TheAsterite' timestamp='1315377106' post='4858503']
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.
[/quote]
Just a little code example of what my code is kind of like with pugixml and XPath:

[code]#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;
}
}[/code]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='TheAsterite' timestamp='1315377106' post='4858503']
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.
[/quote]

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

Share this post


Link to post
Share on other sites
I've used [url="http://www.liquid-technologies.com/xml-editor.aspx"]Liquid XML Editor[/url] in the past which has a pretty decent xml parsing feature plus all the other usual fetaures like data binding, validation etc

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement