16

I'm trying to read an XML file in my C++ program. The XML file looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<myprogram>
<configuration>
<window>
<height> 300 </height>
<width> 500 </width>
</window>
</configuration>
</myprogram>

Right now I can look at the XML file and try to read it like this:

ifstream in("mydata.xml");

//ignore the <?xml line
in.ignore(200, '\n');

//i know that the first value i want is the window height so i can ignore <myprogram> <configuration> and <window>

//ignore <myprogram>
in.ignore(200, '\n');

//ignore <configuration>
in.ignore(200, '\n');

//ignore <window>
in.ignore(200, '\n');

string s; int height;

//okay, now i have my height
in >> s >> height;

In general this seems like a bad idea and it really limits how the XML file can be modified. The above solution is very manual and if anything in the XML changes it seems that the entire method of reading it would have to be changed.

Is there a better way to do this?

user974967
  • 2,928
  • 10
  • 28
  • 45
  • I havn't used c++ in years so I cant name one but google an xml parser for c++ and I am sure you will find something. – Dan675 Feb 27 '12 at 22:21

5 Answers5

6

You could use some library that will do it for you. If you are working on Windows platform, you could use MSXML which is part of the system already.

Check this question: Read Write XML File In C++

Other popular libraries: , ,

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
5

You will need a XML Parser. There are a bunch out there:

My personal favorite is pugiXML but that is a question of personal preference.

pmr
  • 58,701
  • 10
  • 113
  • 156
2

boost property tree works very well with xml, I would use that.

01100110
  • 2,294
  • 3
  • 23
  • 32
  • 1
    I don't think you gain much over the RapidXml library which Boost uses for property tree. Unless you're already using Boost, this would add a big dependency instead of a single header file (RapidXml). – Tomas Andrle Mar 06 '12 at 16:45
  • indeed, it all depends on what libraries people are already using and if they need cross-platform compatibility – ikku100 Feb 16 '15 at 10:32
1

You can use POCO library which has functions for parsing XML

Roman Smelyansky
  • 319
  • 1
  • 13
1

In multi-platform source I usually use Qt XML reader.

You have 3 ways to read:

  1. Qt core QXmlStreamReader - Qt way of XML reading
  2. SAX2 reader - standard SAX2 reader with content handling class
  3. DOM reader - DOM document reader with XML nodes

If you write Windows only software, you should use MSXML 6. Since Windows XP SP3 MSXML 6.0 is part of the OS.

On Linux you should use libxml2.

Naszta
  • 7,560
  • 2
  • 33
  • 49
  • Why should he settle with a C XML library when he can have a C++ one? – pmr Feb 27 '12 at 22:54
  • @pmr: why not? In many case pure C libraries are faster. libxml2 is commonly used and it is usually on the machine, so he does not make one more dependency. – Naszta Feb 27 '12 at 22:57
  • In my experience C libraries often feel weird in idiomatic C++ and require some amount of wrapping and don't integrate too well with other code and larger code bases. But that is probably very subjective. – pmr Feb 27 '12 at 23:00
  • @pmr: on Linux many C++ xml reader based on libxml2 (e.g.: [VTK](http://www.vtk.org)'s parser). If you check [this example](http://xmlsoft.org/examples/parse4.c), you will find that this C library is not coding hell. But basically agree, so I usually use Qt's way of parsing. – Naszta Feb 27 '12 at 23:07