CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  1. #1
    Join Date
    May 2015
    Posts
    540

    Parsing xml string

    I have the following input
    "<?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" />"

    and want to parse to get the output in the following format.

    const char output[100] = "450 MHz Default-Basic Model, 900 MHz Default-Enhanced Model";

    I have written following code, any commnets or suggestions helpful
    Code:
    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);
        }
    }
    Last edited by pdk5; September 26th, 2019 at 10:32 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured