954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read XML File in C/C++

HEllo Everybody.....

feels nice to be part of daniweb..community..joined only today......
i am quite ignorant of XML.....but now i have to develop a c/c++ program which reads a XML file......stored on LAN.....retreives the content of the file...and do necesarry manipulation with the retreived data......actually the xml file contains the name of an image and the coordinates of certain points in that image......so the task is to read xml file....then open the corresponding image file and plot the retreived points...in the image.........since i work in image processing..so i have written the code for opening the image and plotting the points...but since i know a little about xml....so i dont know how to reteive the name of image and coordinates from the xml file......
Please help!!!!
Thanks
Paramveer

paramveer
Newbie Poster
8 posts since Jun 2006
Reputation Points: 13
Solved Threads: 0
 

try any one of these , but be prepared to learn lots of COM stuff. This is one case I recommend doing it in VB.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hello
Thanks for replying...actually by C/C++ i meant Visual C++.....so instead of VB.....as you said........cant it be done easily in Visual C++....if possible tell me how to do in Visual C++......i cannot use VB...because rest of my code is in Visual C++.....please tell me by some giving some points...as to what the approach should be....instead of giving google links....
Thanks a LOT for Replying.....

paramveer
Newbie Poster
8 posts since Jun 2006
Reputation Points: 13
Solved Threads: 0
 

>>please tell me by some giving some points...as to what the approach should be....instead of giving google links

The link I posted was incorrect -- see my next post for some good ones.

you really need to learn to do some research on your own. reading xls files isn't a simple task. The file format is a Microsoft secret, subject to change from one version of Excell to another, and requires knowedge of COM because you will be calling some Excell functions to manipulate the file, you don't do it in your program directly because you will not know the file format. If you really want to do that there is information out there that you will need to study. google is the FIRST place you should be looking and maybe a book from www.amazon.com or your local book store.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

The link should have been this one. But then you could have easily found that out yourself.

And here are some others about Excel Automation

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Um. I think you got it confused dragon. He is infact looking to read an XML file. So I think your first link is correct.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 
Um. I think you got it confused dragon. He is infact looking to read an XML file. So I think your first link is correct.

Well, in that case forget what I posted about Excell Automation -- it has nothing to do with XML files. Guess I must be getting senile in my old age:mrgreen:

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I think Boost Libraries has some xml parsing functions that might help

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hello Everybody....
But what about the way ...how to do it....googgle gives results for xml parsers etc....please tell me how to proceed by maybe giving some points....
Thanks

paramveer
Newbie Poster
8 posts since Jun 2006
Reputation Points: 13
Solved Threads: 0
 

I am totally confused please tell me how to proceed....
Thanks....

paramveer
Newbie Poster
8 posts since Jun 2006
Reputation Points: 13
Solved Threads: 0
 

Here is an XML file and a C++ program that processes it. XML file structure is user defined. So this won't work for your XML file. But you can get an idea on how to proceed by studing this example. Now you figure how to do it for your XML file on your own.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

This c++ example from boost looks promising. Never used it myself.

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Are you using one specific XML file?
Is this file going to be changing over a period of time?
If so, will you have to deal with the changes?

Please answer the questions.

in the meantime, you should be able to view the xml file by:
1 copy to desktop
2 change file extention to .txt instead of .xml
3 you may then read contents with a text editor
(notepad worked for me)
This may give you more insight about what you are up against.
Here is contents of a small xml file I found on my computer.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<options>
  <scheme ID="main">Vegas</scheme>
  <template ID="main"></template>
  <bufferspace ID="main">5</bufferspace>
  <candlewidth ID="main">3</candlewidth>
  <lastbox ID="main">False</lastbox>
  <lastlinecolor ID="main">False</lastlinecolor>
  <manualcandle ID="main">False</manualcandle>
  <zoomed ID="main">False</zoomed>
  <zoompoints ID="main">20</zoompoints>
</options>
huffstat
Newbie Poster
19 posts since May 2006
Reputation Points: 10
Solved Threads: 0
 

Hello Wolfpack and Ancient Dragon,
Thanks for your reply...it was useful.....it works...but what if each time i need to load a different XML file...say...xyz1 then xyz2 then xyz3 and so on.....i.e open first xml file(xyz1) read it open next(xyz2)....and so on but in a loop so could you tell me how to increment the name of xml file in each loop..i.e xyz1...then xyz2.....which C functions can do this
Thanks

paramveer
Newbie Poster
8 posts since Jun 2006
Reputation Points: 13
Solved Threads: 0
 
what if each time i need to load a different XML file...say...xyz1 then xyz2 then xyz3 and so on.....i.e open first xml file(xyz1) read it open next(xyz2)....and so on but in a loop so could you tell me how to increment the name of xml file in each loop..i.e xyz1...then xyz2.....which C functions can do this Thanks

You will have to create the filename by using a function like sprintf or snprintf . snprintf is much safer.
something like

for ( int file_number = 1; file_number<= 3; file_number++ )
{
     char filename[ 20 ] = "";
     snprintf( filename, 19, "xyz%d.xml", file_number );
     printf("%s\n", filename );
     // pass filename to your xml file opening function
}


should do the trick.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Thanks a lots...i worked with similar sprintf() function in MATLAB but didnt know similar function exists in C also....
anyways relating to the xml code link that you gave that day.....it was in German(maybe)..so i couldnt understand the comments...so about strtok()...i searched it on google... it said that it is a parser which looks for a given delimiter in a string like <> in your case XML
... but the initial code in that xml file said for(i=0;i<5;i++) but you corrected it to for(i=0;i<3;i++) and the program wirked ...could you tell me what was the problem with initial code...........Please
Thanks

paramveer
Newbie Poster
8 posts since Jun 2006
Reputation Points: 13
Solved Threads: 0
 
Thanks a lots...i worked with similar sprintf() function in MATLAB but didnt know similar function exists in C also....


MATLAB is more or less like C, so most functions in MATLAB are available in C.anyways relating to the xml code link that you gave that day.....it was in German(maybe)..so i couldnt understand the comments...
Damned if I knew either. More like Dutch I suppose.... but the initial code in that xml file said for(i=0;i<5;i++) but you corrected it to for(i=0;i<3;i++) and the program wirked ...could you tell me what was the problem with initial code...........Please
Thanks
The line that should be parsed was <Pindala> 83858km² </Pindala><Rahvaarv>8 116 000</Rahvaarv>
So after searching for the Word Pindala , we should stop at the word Rahvaarv , because that is processed in the next part.

if (strcmp(pos, "Rahvaarv") == 0)


That means starting from the word P, how many delimiters should we parse to get to Rahvaarv? <strong><</strong>Pindala<strong>></strong> 83858km² <strong><</strong>/Pindala><Rahvaarv>8 116 000</Rahvaarv>
The answer is 3. The ones in Blue are taken care of by the pos = strtok(rida,"<>"); at the beginning.
So you should have 3 strtoks.
Those 3 are

if (strcmp(pos, "Pindala") == 0)
			{
				for (i=0;i<3;i++)
				{
					pos = strtok(NULL,"<>"); // Called 3 times


So that makes 3. If you put 5, it would stop somewhere after Raahvarv and you will not be able to parse the values needed in the next if statement. Anyway I suggest you look at how strtok conducts it's search given in this page . That will make it more clearer.

Also note that I didnt look at the code much at that time; I only wanted to get him going, so even now I can see a lot of cleaning needs to be done in it. For example
if ((pos != NULL)&&(k==0)) does not need the check for k, and i++; can be safely removed from the code. You can clean up the code before you use it. That makes understanding the code much easier. Oh well.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Hello,
I couldnot understand the working of strtok().....does it parse the string in <>.....then how come it displays the names of countries etc. which are not in <>......or it parses only 1 delimiter < at a time......but then why do we write strok(s1,"<>"); .....i followed the link you gave me but i couln ot understand its working so could you throw some light on this topic.......
Thanks

paramveer
Newbie Poster
8 posts since Jun 2006
Reputation Points: 13
Solved Threads: 0
 
Hello, I couldnot understand the working of strtok().....does it parse the string in <>.....then how come it displays the names of countries etc. which are not in <>......or it parses only 1 delimiter < at a time......but then why do we write strok(s1,"<>"); .....i followed the link you gave me but i couln ot understand its working so could you throw some light on this topic....... Thanks

I suggest you try parsing just one line instead of the whole file, and see what are the contents pointed by pos, and the original array step by step by adding some printf statements. Then try to see how that agrees with the explaination given by that site. You will have a better chance of understanding the code that way.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 
I think Boost Libraries has some xml parsing functions that might help

Hi please help me, I want to read an xml file with c++. Means, using COM+ logics how I can send my xml file in the local directory to my c++ code to read it? It would be really thankful if you could give me some ideas about that.

RamPrasad710
Newbie Poster
3 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You