Reputation: 176
I am trying to read an xml file in c++ without using any c++ libraries. I have taken code from another post and tried to understand and implement it. So far I have this...
{
string line;
ifstream in("Team.xml");
bool startOfTag;
startOfTag = false;
while (getline(in,line))
{
std::string temp;
for (int i = 0; i < line.length(); i++)
{
if (line[i] == ' ' && temp.size() == 0)
{
}
else
{
temp += line[i];
}
}
cout << "-->" << temp << "<--" << endl;
if (temp == "<Fname>")
{
cout << "Found <Fname>" << endl;
startOfTag = true;
continue;
}
else if (temp == "</Fname>")
{
startOfTag = false;
cout << "Found </Fname>" << endl;
}
if (startOfTag)
{
cout << temp << endl;
}
}
}
then, I have my xml file looking like this.
<?xml version="1.0" ?>
<Team>
<Player>
<Fname>David</Fname>
<Lname>James</Lname>
</Player>
</Team>
At this moment in time, I am only wanting to read the values within <Fname>
i.e. David but when running the program this I the entire xml contents. Any ideas?
Upvotes: 0
Views: 1816
Reputation: 57749
I suggest you follow the syntax and grammar as defined by the XML specification:
You should also invest in a debugger or if your IDE supports breakpoints and single stepping, use it.
I don't understand what your for
loop is trying to accomplish.
If you are going to parse a language, do it properly. Look up "how to parse" or "lexical analysis".
The following methods of std::string
may be of use:
find
substr
find_first_not_of (good for skipping whitespace)
Upvotes: 1