I am working in my study with xml file therefore I want to read an xml file with c++ to get the information such the coordinates of the points in order to use them later.
I have written the following code, but I have just taken only one value. I don't know where is the problem.
The xml file is

<Amplitude2d U="0.0000000000E000" V="0.0000000000E000"/>
<Node2d U="2.5100000000E003" V="1.4000000000E003"/>
<Amplitude2d U="0.0000000000E000" V="0.0000000000E000"/>
<Node2d U="5.7100000000E003" V="1.4000000000E003"/>
<Amplitude2d U="0.0000000000E000" V="0.0000000000E000"/>
<Node2d U="5.7100000000E003" V="2.8000000000E003"/>
<Amplitude2d U="0.0000000000E000" V="0.0000000000E000"/>
<Node2d U="2.5100000000E003" V="2.8000000000E003"/>

I soll extract the values of U and V for Node2d and the values of U and V for Amplitude2d many times as the file contains. I have achieved to get only the first values:

<Amplitude2d U="0.0000000000E000" V="0.0000000000E000"/>
<Node2d U="2.5100000000E003" V="1.4000000000E003"/>

My written code was:

#include <iostream>
#include <math.h>
#include <sstream>
#include <fstream>
#include <string>

using namespace std;

struct point
	double x;
	double y;
	double dx;
	double dy;
struct point p[500];
struct point q[500];

void Read_File (point ip[],point iq[], int &ipcounter,int &iqcounter,char name[])  //  get the coordinates and the number of the p points and q points
	ifstream pts(name); // to read the file
	string s,s1;
if (pts.is_open())

		int xx=s.rfind("<DetailedContour>");  cout<<"xx:  "<<xx<<endl; // the first value at which begin the string s1
		int yy=s.rfind("</DetailedContour>"); cout<<"yy:  "<<yy<<endl; // the second value at which end the string s1
		cout<<" The Text is: \n"<<s1<<endl;
			string xcor1=s1.substr(s1.find("<Node2d U=")+11,16);
			string ycor1=s1.substr(s1.find("<Node2d U=")+32,16);
			string dx1=s1.substr(s1.find("<Amplitude2d U=")+16,16);
			string dy1=s1.substr(s1.find("<Amplitude2d U=")+37,16);

			stringstream ss(xcor1); //set up a stringstream with the first string
			ss >> ip[ipcounter].x; //cout<<pt.x<<endl; //extract the number
			ss.clear();  //reset the stream to the beginning
			ss.str(ycor1); //set the stringtream to the second string
			ss >> ip[ipcounter].y;  //cout<<pt.y<<endl; //extract the second number
			ss >> ip[ipcounter].dx;
			ss >> ip[ipcounter].dy;
	{cout<<"there is a problem with opening the file...."<<endl;}
	for(int i=0;i<ipcounter;i++)
		cout<<"x1:"<<ip[i].x<<"   ";
		cout<<"y1:"<<ip[i].y<<"   ";
		cout<<"dx1:"<<ip[i].dx<<"   ";
void iFile ()
	int pNum=0;
	int qNum=0;
	char *iname;
	iname= new char [20];
	cout<<"Enter the name of the file to be read.."<<endl;  //Plate_2.xml
	Read_File (p,q,pNum,qNum,iname);
int main ()
return 0;

Recommended Answers

All 3 Replies

Member Avatar

That is a pretty crude way to do it?

Any reason why you're not using an xml parser... Or even a language with xml parsing built in?

Thanks for reply

I have not any idea how to use xml parser and furthermore there is only one part in xml file which I am interested in which contains the points which I need. Do you how can I further develop my code in order to get the rest of points.
Thanks in advance

xerces is a reasonably good C++ xml parser.

There's a barrier to learning to use this kind of thing, since you have to put some work into getting things set up, however the benefits are huge. For example, say you want to read a different kind of file, or the format of this type of file changes, then making your program compliant with the changes would be easy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.