Reading a ".xml" file with C++

Hey everyone,
so recently I got the assignment of creating a C++ program that should be able to read and write to a file called "bookdata.xml". Now before you think I'm asking you to do my homework hear me out - the program (I would assume) is ready to be modified to work with XML, and I already understand how to use and write XML. What I need to know is how to read and write to the ".xml" file from the program. For instance, mine should add an entry to the file from a variable of structure book, and be able to read and search the file (and most likely store the contents of the file in an array for removing entries). A little bit of code would be nice but all I need is the general idea.

Edit: If you want some code to better understand the question just tell me if and which bit you would like.
Last edited on
i think this may be what you're looking for:
so you need some library to read xml.. Google: xerces
Well Mal, I understand fstream and actually have experience using it, but what I was hearing was that with XML files and C++ you can search through/go straight to certain elements of the file. Kind of hard to explain, you'll need to be familiar with XML/HTML to understand what I'm saying here.
You cannot go straight to certain elements in an XML/HTML file since there is no way to know where any particular element is (i.e., they are not of equal size and are not indexed). If the file is not too large (the usual case), you would normally read the whole file into a tree structure, process it internally, and then write it out as XML again.
Last edited on
Don't worry son, you'll figure it out.
i think what u need is a method that reads the file from frond to end and saves the positions where it found the matchin start- and end point of the area u need... and then it reads out what is between the marks...
True. If you didn't want to read the data in, you could do a pass through the file and record the positions. But that still requires an initial pass before reading a record; not exactly random access. After that, however, it's good for reading, but somewhat more difficult for writing.
why you all are trying to use fstream to read xml when there are many xml c++ libraries??
Yeah writing is one of the important features, but I would think that the whole thing should be designed around the ability to search since that's probably one of the harder aspects of the program.

I don't really think there are any built in XML libraries, which would require anyone else that would want to use my program (or myself working somewhere else) to download the required libraries as well. However if you guys think it would be a good idea I'll consider it.
if you see above.. i wrote xerces.. its a good xml c++ parser..
there are others also Expat, XmlLite....

because it will be good hard work parsing xml using fstream and not a good idea too..

here is the link:

in the begining it looks a bit difficult..but once you read the documentation of some basic classes it will be a very easy work parsing xml.. you can search/write tags/elements/attributes/values with just a couple of function calls with efficiency.
Last edited on
Topic archived. No new replies allowed.