1 year ago

#357435

test-img

Nells

How to setup a XML Parser parsing and XML document with formatted raw data in C++ without using existing tool

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.

c++

xml

xml-parsing

raw-data

0 Answers

Your Answer

Accepted video resources