Reading a ".xml" file with C++

May 22, 2009 at 10:59pm
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 May 22, 2009 at 11:04pm
May 23, 2009 at 1:14am
i think this may be what you're looking for: http://www.cplusplus.com/reference/iostream/fstream/
May 23, 2009 at 3:07am
so you need some library to read xml.. Google: xerces
May 27, 2009 at 3:09pm
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.
May 27, 2009 at 3:16pm
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 May 27, 2009 at 3:17pm
May 27, 2009 at 11:49pm
Don't worry son, you'll figure it out.
May 28, 2009 at 1:35am
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...
May 28, 2009 at 1:46am
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.
May 28, 2009 at 8:50am
why you all are trying to use fstream to read xml when there are many xml c++ libraries??
May 28, 2009 at 1:56pm
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.

@writeonsharma
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.
May 28, 2009 at 2:04pm
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:
http://xerces.apache.org/xerces-c/


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 May 28, 2009 at 2:06pm
Topic archived. No new replies allowed.