-   Programming (
-   -   Working with strings in an xml file C++ (

vbx_wx 03-07-2011 09:58 AM

Working with strings in an xml file C++
I have a function that retrives text between title and links tags from an XML file, but what i want is to test if the title and link tags are between item tags. This is my code:

istringstream iss(content);
    string line;
    while(getline(iss, line))
        // get <title> and </title> positions
        int found3 = line.find("<title>");
        int found4 = line.find("</title>");
        // get <link> and </link> positions
        int found5 = line.find("<link>");
        int found6 = line.find("</link>");

        // if found tags, add them to stl::list
        if(found3 >= 0 && found4 >= 0)
            string getTitleStr = line.substr(found3 + 7, found4 - found3 - 7);
        if(found5 >= 0 && found6 >= 0)
            string getLinkStr = line.substr(found5 + 6, found6 - found5 - 6);

Does anyone have an ideea how to do this with C++ strings only ? Without parsers.And yes, i know its paitfull to implement without parsers
Thank you.

dwhitney67 03-08-2011 05:42 AM

Please show an example of the XML file that contains the data you are attempting to parse; your opening statement was not very clear.

And btw, the STL string find() function returns a size_t value, not an int. The returned value will either be a usable index, or std::string::npos on failure. Thus checking if the return value is greater than zero (0) is not appropriate.

All times are GMT -5. The time now is 01:44 PM.