I need help by opening an xml file

I am working now with xml files and I want to extract some information without need to parse the whole file. Therefore I used the string and I/o stream objects, but when I do that I can only get one value which i interested in. Is there anybody who can help me to fine the problem my code is like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//20.05.11
// read a xml File
#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("point.txt");
//ifstream pts("point1.txt");
ifstream pts(name); // to read the file
string s,s1;
ipcounter=0;
iqcounter=0;
//string::const_iterator iterator1= s1.begin();
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;
while (???????) //I don't know what should write here
{
//getline(fin,s1);
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();
}

Thanks in advance
Something needs to parse the XML. That's the sort of thing best left to an XML library.

TinyXML is as simple as they come: http://www.grinninglizard.com/tinyxml/ There are examples here: http://software.ellerton.net/tinyxml.html
Do you know how that compares to RapidXML? I have being using RapidXML as it's fast and easy. However, it's not W3C compliant so it might outstay it's welcome...
Thanks for reply,
I have said, that I don't want to use a parser. I just want to extract a few values to use them later. I haven't any idea how this parser works, therefore it is better to do that with string and I/o files as I have already written in my above all code.
I have said, that I don't want to use a parser. Then why bother using XML?
You can use a simple text file instead.
Last edited on
To make the code simpler, i think you sould first read the whole file line by line in a single string variable, then search your elements in this single string.

What is the element you want to load multiple times? DetailedContour? Node2d?

Also, maybe you should use regular expression for your searches. An additional space in the wrong place, and your parsing will fail. In one year you (or the user) will have forgotten that it's the case, will write a xml file with a legal syntax, say <Node2d U = ".... , and won't understand why it doesn't work (trust me)
Thanks for reply,
You can use a simple text file instead.

I should take my points coordinates from an xml file , while this kind of files is supported by an CAD program.
My xmlfile is like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
.
.
.
.
.
.
.

.
</Segment2d>
<Segment2d>
<Amplitude2d U="0.0000000000E000" V="0.0000000000E000"/>
<Node2d U="3.9100000000E003" V="4.6000000000E003"/>
</Segment2d>
<Segment2d>
<Amplitude2d U="0.0000000000E000" V="0.0000000000E000"/>
<Node2d U="2.5100000000E003" V="4.6000000000E003"/>
</Segment2d>
</SimpleContour>
<DetailedContour>
<StartPoint2d U="2.5100000000E003" V="4.6000000000E003"/>
<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="3.9100000000E003" V="1.4000000000E003"/>
</Segment2d>
<Segment2d>
<Amplitude2d U="0.0000000000E000" V="0.0000000000E000"/>
<Node2d U="3.9100000000E003" V="4.6000000000E003"/>
</Segment2d>
<Segment2d>
<Amplitude2d U="0.0000000000E000" V="0.0000000000E000"/>
<Node2d U="2.5100000000E003" V="4.6000000000E003"/>
</Segment2d>
</DetailedContour>
</Plate>
</PlateGroup>
</PlanePanel>
</Block>
</Ship>

and I am interested in only the values of Detailed contour. In my above code I have used a string to extract the only a part of the xml file and later take the values of Amplitude U, V and values of Node2d U and V.
But the problem is when I extract the values from the string s1. In this section I need help.
Topic archived. No new replies allowed.