Hello
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

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

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;
	ipcounter=0;
	iqcounter=0;
if (pts.is_open())
	{
		getline(pts,s);

		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
		s1=s.substr(xx,(yy-xx));
		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.clear();
			ss.str(dx1);
			ss >> ip[ipcounter].dx;
			ss.clear();
			ss.str(dy1);
			ss >> ip[ipcounter].dy;
		
			ipcounter++;
		}
						
	}
	else 
	{cout<<"there is a problem with opening the file...."<<endl;}
	
	cout<<"ps................."<<endl;
	for(int i=0;i<ipcounter;i++)
	{
		cout<<"x1:"<<ip[i].x<<"   ";
		cout<<"y1:"<<ip[i].y<<"   ";
		cout<<"dx1:"<<ip[i].dx<<"   ";
		cout<<"dy1:"<<ip[i].dy<<endl;
	}
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
	cin>>iname;
	Read_File (p,q,pNum,qNum,iname);
}
int main ()
{
iFile();
cin.get();
return 0;
}
Member Avatar for iamthwee

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, networking, learning, and sharing knowledge.