-4

The output string xml has list of

<?xml version="1.0" encoding="utf-8"?>
<MODEL-LIST>
    <MODEL ID="Default1" TYPE="Enhanced Model" />
    <MODEL ID="Default2" TYPE="Basic Model" />
</MODEL-LIST>

Now I want to filter this output XML, into a string in the format char * output = "Enhanced Model-Default1, Basic Model-Default2". Do you think it is good to go for another transform or directly parse to C++ class/struct ?

user3903854
  • 11
  • 1
  • 5
  • What is your real goal and what are your problems? – RoQuOTriX Sep 25 '19 at 08:35
  • Your question is unclear. Please provide a [mcve]. Maybe this will help to understand the question. – Thomas Sablik Sep 25 '19 at 08:37
  • It seems it's time you refresh [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Sep 25 '19 at 08:38
  • If you don't want to write the XML parser by yourself (I wouldn't recommend this), here is a list of some available: [What XML parser should I use in C++?](https://stackoverflow.com/a/9387612/7478597) – Scheff's Cat Sep 25 '19 at 08:41

1 Answers1

0

I have comeup with answer myself, which works, but code needs cleanup: It maynot be elegant but works. Any suggestions , improvements will help me to improve myself.

Actual input I had is

const char* buf = xmlStr;
int bstrlen = xmlStr.length();
std::string str(buf ? buf : "", bstrlen);

// Vector of string to save tokens 
vector <string> tokens;
vector <string> models;

// stringstream class check1 
stringstream check1(str);

string intermediate;

// Tokenizing w.r.t. space ' ' 
while (std::getline(check1, intermediate, '\n'))
{
    tokens.push_back(intermediate);
}

for (string token : tokens)
{
    std::string first = "MODEL ID=\"";
    std::size_t firstLim = token.find(first);      

    std::string last = "\" TYPE=\"";
    std::size_t lastLim = token.find(last);

    if ( (firstLim != std::string::npos) && (lastLim != std::string::npos) )
    {
        std::string modelid = token.substr(firstLim, lastLim - firstLim);

        int i = first.size();

        modelid = modelid.substr(first.size());

        models.push_back(modelid);

        std::string modeltype = token.substr(lastLim);
        modeltype = modeltype.substr(last.size());

        std::string elem = "\"";
        std::size_t elemLim = token.find(elem);
        modeltype = modeltype.substr(elemLim);
    }
}

Exact input is :

 "<?xml version=\"1.0\"?>\r\n<MODEL-LIST>\r\n<MODEL ID=\"450 MHz Default\" TYPE=\"Basic Model\" />\r\n<MODEL ID=\"900 MHz Default\" TYPE=\"Enhanced Model\" />"
user3903854
  • 11
  • 1
  • 5