reading xml weird error

hello I am trying to read this xml file


this readFile function does not give me right values. Only to use cstring..no pointers or other libs such as xml libs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void readFile(char file[], ifstream& read){
	char line[chr_len];
	read.open(file, ios::in);
	
	if(read.fail())
		cout<<"Sorry could not read";
	
	else{
		
		while (read.good()){
			read.getline(line, '\n');
			
			cout<<line<<endl;
	
			}
		
		}
	read.close();	
	
	}


<?xml ver


here is my file

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
<?xml version=”1.0”?>
<address_book>
	<contact>
		<name>Hoo Zheng Qi</name>
		<street>10 Jalan SS15</street>
		<city>Subang Jaya</city>
		<state>Selangor</state>
		<postcode>41500</postcode>
	</contact>
	<contact>
		<name>Lai Yik Sheng</name>
		<street>9 Jalan Sentosa 2</street>
		<city>Shah Alam</city>
		<state>Selangor</state>
		<postcode>47400</postcode>
	</contact>
	<contact>
		<name>Sanjeet Singh</name>
		<street>5 Penang Road</street>
		<city>Georgetown</city>
		<state>Pulau Pinang</state>
		<postcode>32800</postcode>
	</contact>
	<contact>
		<name>Jeremiah Htet Thu Htwe</name>
		<street>98 Lorong Gugusan Alam</street>
		<city>Petaling Jaya</city>
		<state>Selangor</state>
		<postcode>48000</postcode>
	</contact>
</address_book>


Also how can i only read the values..not the tags?
Last edited on
I think there are some XML libraries/parsers out there.
Like this one:
http://libxmlplusplus.sourceforge.net/docs/manual/html/
i m not supposed to used xml libraries
So "<?xml ver" is all that is printed? Is chr_len big enough?

Note that XML files does not necessary have one tag per line. Newlines are just another whitespace character, like space and tab.
yes chr size is 30..also how to ignore the tags..only values I want to read in struct
Last edited on
It appears that chr_len is 10, not 30. Can't confirm that though since you haven't posted your full code.

how to ignore the tags

You're going to have to write code to do that. However, you don't want to ignore the tags completely. The tags are what identify the data in your file and give it structure.

I need to write function which take chracter city name and displays the contacts living in that city..how should I do this? do i write all records in a struct first? or do i do this while reading from file?
do i write all records in a struct first? or do i do this while reading from file?

You can do it either way. Displaying the data while reading the file is easier since you only have to deal with one record at a time.
Pls tell me what to do in taglessline function

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
void readFile(char file[], fstream& read){
	char line[1000];
	char word;
	read.open(file);
    unsigned short int i=0;
    if(read.fail())
		cout<<"Sorry could not read";

	else{
        //read.ignore(41);
		while (read.good()){
            read.getline(line,1000);
            cout<<line<<endl;
            //taglessLine(line);
			}

		}


	read.close();

	}

void taglessLine(char line[]){

    int len=strlen(line);
    char tagl[30];
    for(int i=0; i<len;i++){


        }
}
Last edited on
Topic archived. No new replies allowed.