1

I have hit a wall with my current skill level (started with C++ mid Feb of 2022). I need to setup a manual XML Parser that reads a XML file that always remains the same in its format but only saves measurements inserted by a XML writer. With manual parser I mean that I have to create it myself and am not allowed to use exiting tools like TinyXML for licensing and complication reasons. And I believe in order to have simplicity and a fast cycle time.

The requirements are:

  • create a manual parser myself and do not use something like tinyXML
  • prefered method is the DOM method
  • the parser should insert the each tag from existing xml into its coresponging key into an existing map

All I could find is the code below. I do not have anyone who could help me atm and am hoping I could grab some advice here to get this thign going.

I have studied C++ basics up to the topic of pointers on CodeAcademy so go easy on me haha

This is the XML document that I am trying to read

<History_24h>
    <Level1>
        <Level1 ID = 2>
            <Level2>
                <Level2 ID = 4>
                    <Level3>
                        <Level3 ID = 8>
                            <Level4>
                                <Level4 ID = 16         time:  1648622389473    value:  8888.000000</Level4>
                            </Level4>
                        </Level3>
                    </Level3>
                </Level2>
            </Level2>
        </Level1>
    </Level1>
</History_24h>
<History_24h>
    <Level1>
        <Level1 ID = 2>
            <Level2>
                <Level2 ID = 4>
                    <Level3>
                        <Level3 ID = 8>
                            <Level4>
                                <Level4 ID = 16         time:  1648703367954    value:  8888.000000</Level4>
                            </Level4>
                        </Level3>
                    </Level3>
                </Level2>
            </Level2>
        </Level1>
    </Level1>
</History_24h>

This is the sample code that I found on the URL below

https://www.educba.com/c-plus-plus-xml-parser/?source=leftnav

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;
string getdata( string filename );
vector<string> getinfo( const string &content, string tag );
void sto( string &text );

int main()
{
string filename = "class.xml";
string tag = "name";
// string tag = "price";
bool sto = true;
string content = getFile( filename );
vector<string> all = getData(content, tag );
for ( string &a : all )
{
if ( sto ) sto( a );
cout << a << '\n';
}
}
string getdata( string filename )
{
string buff;
char ch;
ifstream in( filename );   if ( !in ) { cout <<get filename << " not found";   exit( 1 ); }
while ( in.get( ch ) ) buff += ch;
in.close();
return buff;
}
vector<string> getinfo( const string &content, string tag )
{
vector<string> take;
unsigned int sop = 0, st;
while ( true )
{
st = content.find( "<" + tag, pos );   if ( st == string::nsop ) return take;
st = content.find( ">" , start );
st++;
pos = content.find( "</" + tag, st );   if ( pos == string::npos ) return take;
take.push_back( content.substr( st, sop - st) );
}
}
void sto( string &text )
{
unsigned int sta = 0, ppp;
while ( sta < text.size() )
{
sta = text.find( "<", sta );    if ( sta == string::ppp ) break;
pos   = text.find( ">", sta );    if ( pos   == string::nsop ) break;
text.erase( sta, pos - sta + 1 );
}
}

I modified the code a little. My code has a few errors that confuse me. This is my code in VS

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

string getdata(string filename);
vector<string> getinfo(const string& content, string tag);
void sto(string& text);


int main()
{
    string filename = "D:\Projects\class.xml";
    string tag = "name";
    // string tag = "price";
    bool sto = true;
    string content = getdata(filename);
    vector<string> all = getinfo(content, tag);
    for (string& a : all)
    {
        if (sto) sto(a);
        cout << a << '\n';
    }
}

vector<string> getinfo(const string& content, string tag)
{
    vector<string> take;
    unsigned int sop = 0, st;
    while (true)
    {
        st = content.find("<" + tag, pos);   if (st == string::nsop) return take;
        st = content.find(">", start);
        st++;
        pos = content.find("</" + tag, st);   if (pos == string::npos) return take;
        take.push_back(content.substr(st, sop - st));
    }
}
void sto(string& text)
{
    unsigned int sta = 0, ppp;
    while (sta < text.size())
    {
        sta = text.find("<", sta);    if (sta == string::ppp) break;
        pos = text.find(">", sta);    if (pos == string::nsop) break;
        text.erase(sta, pos - sta + 1);
    }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

string getdata(string filename)
{
    string buff;
    char ch;
    ifstream in(filename);   if (!in) { cout << get filename << " not found";   exit(1); }
    while (in.get(ch)) buff += ch;
    in.close();
    return buff;
}

I believe that the provided code from the URL wasnt complete nor fully tested. The errors I am getting and I am not sure how to fix them are:

  • pos, start are undefined... I am not sure what datatype to use to define them. auto did not work
  • ppp, nsop has no member... completely lost here.
  • line 61 in my modified code has cout << get filename << in it. this might be a simple fix by using the gets() function????

Big thanks to everyone who can point me into the right direction.

Nells
  • 21
  • 5
  • Take a paus, go back to the requirements, and do a proper analysis on them. Then from the analysis create a design. Spend a lot of time iterating over the requirements, analysis and design before even writing any code. Pen and paper are very important tools here. And once you have a design you're happy with, and is about to implement it in code, you need good knowledge of the basic computer-science data-structures and algorithms, and how the C++ standard library can help you with them (if it doesn't have exactly what you need already). – Some programmer dude Mar 31 '22 at 05:57
  • Also, the design should be very detailed, and each little details should be possible to implement and test all by itself. When writing code, do it in very small increments, set up weekly milestones of tasks you should do, and make sure that all your code builds without warnings (enable extra warnings and treat them as errors), and make sure each little piece of code works before starting with the next piece of code. And use a VCS like Git (and perhaps a repository host like Github) to make sure each and every piece of code can be fully traced, and possible to rollback. – Some programmer dude Mar 31 '22 at 06:00
  • This is the challenge ----> **good knowledge of the basic computer-science data-structures and algorithms** I am required to get this done but it aint making sense to me especially since I dont know how exactly a parser works step by step and what concepts I need to study. I have reasearched the life out of it and so far not found a way yet. I'll keep trying. cheers – Nells Mar 31 '22 at 07:00
  • You have been studying C++ for less than two months, which means you're still in the early beginners phase. This assignment or exercise is *not* a beginners assignment. Especially considering that the shown XML isn't even correct. Who gave you this assignment? What have he or she taught you? You might need to speak more to that person, and ask for clarifications and help. – Some programmer dude Mar 31 '22 at 07:08
  • I agree :-) but I want to try to find a solution for it. Aquiring a new skill can be painful ^^ The example xml has the exact structure that we need. Its already been checked by that person. I know that this problem is above my weight class but I dont really have another option than to give it my best shot. Telling the person that I cant do it is unfortunately not an option. And getting more help or hints didnt work so far. Might be a communications problem... i dunno... What would you say is wrong about the sample XML? – Nells Mar 31 '22 at 07:16
  • There are at least two problems with the shown XML: First of all node attributes must be quoted. For example `` should be ``; The second problem is that the XML have mismatching opening and closing tags, with ``. And regarding those innermost `` nodes, that's not how XML handles multiple attributes. So the file is XML-*like*, but not actual XML. The double-nesting of "levels" also seems like a mistake. Instead of ` ... ` it could (and probably should) be ` ... ` – Some programmer dude Mar 31 '22 at 07:21
  • Ouh btw the sample XML inserts from a multidimensional map – Nells Mar 31 '22 at 07:25
  • Ok that makes sense. My goal was to make those attributes dynamic. They change depending on which hardware in each level is triggered. Was this the wrong way of doing that? I mean the person had a look I just dunno how thorough. – Nells Mar 31 '22 at 07:40

0 Answers0