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

Thread: Parsing xml string

  1. #1
    Join Date
    May 2015
    Posts
    224

    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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: Parsing xml string

    Does this compile - """ (and others similar) isn't valid - it should be """. Why do you need the vectors? Can't you just parse intermediate direct and remove the for loop - and have an 'output' string to which the required output is appended? first, last and elem are const so should be specified as such and initialised once outside of the loop. You define i but don't use it.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: Parsing xml string

    Try this - much simpler!

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    using namespace std;
    
    const string xmlStr = "<?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\" />";
    const string mfirst = "MODEL ID=\"";
    const string mlast = "\" TYPE=\"";
    const string melem = "\" />";
    
    int main()
    {
    	stringstream check1(xmlStr);
    	string output;
    
    	for (string token; getline(check1, token); ) {
    		const auto firstLim = token.find(mfirst);
    		const auto lastLim = token.find(mlast);
    		const auto mend = token.find(melem);
    
    		if ((firstLim != string::npos) && (lastLim != string::npos) && mend != string::npos) {
    			output += (output.empty() ? "" : ",") + token.substr(firstLim + mfirst.size(), lastLim - firstLim - mfirst.size());
    			output += "-" + token.substr(lastLim + mlast.size(), mend - lastLim - mlast.size());
    		}
    	}
    
    	cout << output << endl;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  4. #4
    Join Date
    May 2015
    Posts
    224

    Re: Parsing xml string

    Thankyou very much kaud. Very much appreciated.

    I can never be good programmer, I guess. I simply donot have skill to write good code.

    Not sure how i can improve myself

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: Parsing xml string

    Not sure how i can improve myself
    By writing code and reading code written by others. Unless someone is especially talented, there's really no short cuts. Yes, you need to know the capabilities of the language (syntax, features etc etc) but this is just part (and can be looked up on a web site like cppreference https://en.cppreference.com/w/). The other aspect is having a feel as to how the features of a language can be used to produce what is required. In this case, much use is made of .find() and .substr(). Act like the code and do it on paper. Write down the input, values returned by .find() and work out what is needed for .substr() to return what is wanted. That is what I did when I changed the code. When C++17 came out and MS VS started to incorporate them, I wrote smallish test programs to 'try them out' - get the syntax right and see their capabilities. Then I used them in larger programs etc etc until I became familiar with them. After a while, you get a 'feel' for programming and it starts to come together. When you have some code that works, look at how it could be improved. Criticise your own code and look at others and critically appraise it. After time, you will see your own skills improve. With a complex language like c++ I reckon about 2 years of pretty continuous use to become good programmer.

    Keep coding!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  6. #6
    Join Date
    May 2015
    Posts
    224

    Re: Parsing xml string

    Thanks a lot kaud. I come from electronics background and programming was mindblock for me. But because of job had to study c and then c++. Eventhough i guess i understand features. I find it difficult to apply these features of a language effectively. May be i need to put more effort and thanks a lot for the great help and the suggestions. Very helpful indeed

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: Parsing xml string

    PS Regarding the code in post #3. if your compiler supports it, then it would be easier to read the escaped strings using raw mode - as " then isn't escaped. Consider:

    Code:
    const string mfirst = R"(MODEL ID=")";
    const string mlast = R"(" TYPE=")";
    const string melem = R"(" />)";
    NB. Don't try it with xmlStr as then /r/n isn't converted to a new line!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: Parsing xml string

    Quote Originally Posted by pdk5 View Post
    Thanks a lot kaud. I come from electronics background and programming was mindblock for me. But because of job had to study c and then c++. Eventhough i guess i understand features. I find it difficult to apply these features of a language effectively. May be i need to put more effort and thanks a lot for the great help and the suggestions. Very helpful indeed
    When I did electronics, I struggled to design circuits to get the required outcome. That never came easy to me the way some could 'just throw a few components together' and hey-presto it worked! Never did for me first, second or sometimes even third go.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  9. #9
    Join Date
    May 2015
    Posts
    224

    Re: Parsing xml string

    Thanks a lot kaud for the support and encouraging words..hopefully i should be able to pickup , may be in bit more tries and effort

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)