Help with Using string class member functions please

Hi here's my task an below is most of the code already done. Thanks for any guidance:


Develop the program further so that it finds and extracts specified items from the xmlstring using start and end tags. Now we find and extract and display first the location information and then the temperature information. Location can be found between the tags <location> and </location>. The temperature is between the tags <temp_c> and </temp_c>.

To make it easy to find whatever information from the, xml-string write a function that takes the xml-string and the "inner" text (same for start tag and end tag) of the tags as parameters and returns the text from between the start tag and end tags. If either start or end tag is not found the function must return "not found". Note that when you search for the tag you must search for the whole tag (including angle brackets) not just the tag name that was given as parameter.

For example, if you wanted to find the location
location = find_field(page, "location");

and to get the temperature you could call it as follows:
temperature = find_field(page, "temp_c");


MY CODE: sorry the code format tool is not working for me


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string find_field(const string& xml, string tag_name);


int main() {
string page, line, location, temperature;
ifstream inputFile("weather.xml");

while (getline(inputFile, line)) {
page.append(line);
line.erase();
}

location = find_field(page, "location");
temperature = find_field(page, "temp_c");

cout << "Location: " << location << endl;
cout << "Temperature: " << temperature << endl;
}


string find_field(const string& xml, string tag_name)

{
string start_tag = "<" + tag_name + ">";
string end_tag = "</" + tag_name + ">";

return "not found";
}

Last edited on
string has a find function. See:

http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/string/string/substr/

if there are no spaces to consider you can use it like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string find_field(const string& xml, string tag_name)

{
string start_tag = "<" + tag_name + ">";
string end_tag = "</" + tag_name + ">";

const size_t pos_start = xml.find(start_tag);
if(pos_start != string::npos)
{
  const size_t pos_end = xml.find(end_tag);
  if(pos_end != string::npos)
    return xml.substr(pos_start, pos_end - pos_start);
}

return "not found";
}
Not tested!
.find() returns the position of the beginning of the found string.

Consider C++17:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

string find_field(const string& xml, const string& tag_name);

int main() {
	ifstream inputFile("weather.xml");

	if (!inputFile)
		return (cout << "Cannot open file\n"), 1;

	string page;

	for (string line; getline(inputFile, line); page += line);

	const string location {find_field(page, "location")};
	const string temperature {find_field(page, "temp_c")};

	cout << "Location: " << location << '\n';
	cout << "Temperature: " << temperature << '\n';
}

string find_field(const string& xml, const string& tag_name)
{
	const string start_tag {"<" + tag_name + ">"};
	const string end_tag {"</" + tag_name + ">"};

	if (const size_t pos_start = xml.find(start_tag); pos_start != string::npos)
		if (const size_t pos_end = xml.find(end_tag, pos_start + start_tag.size()); pos_end != string::npos)
			return xml.substr(pos_start + start_tag.size(), pos_end - pos_start - start_tag.size());

	return "";
}

Topic archived. No new replies allowed.