LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-07-2011, 09:58 AM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
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:
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);
            titles.push_back(getTitleStr);
        }
        if(found5 >= 0 && found6 >= 0)
        {
            string getLinkStr = line.substr(found5 + 6, found6 - found5 - 6);
            links.push_back(getLinkStr);
        }
    }
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.
 
Old 03-08-2011, 05:42 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM
[SOLVED] Reading in a file in C, after file read null terminator at strings end gone josip76 Programming 2 05-14-2010 12:44 AM
Searching .txt file for (specific) strings and printing them to new file Hb_Kai Linux - General 7 02-18-2010 09:09 AM
guidlines on working with strings in C? zero79 Programming 1 02-22-2005 03:54 PM
Perl>>working with strings format_c Programming 2 11-15-2001 04:21 PM


All times are GMT -5. The time now is 12:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration