By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Parsing question

P: n/a
If I have the following string from a huge xml file:

std::string s = "<input key1=\"v1\" key2=\"val4\" key3=\"test\" />";

I need to get the values associated with the keys out from this line.
What is the best way to do this? I was looking at sscanf, and
std::string find, but this seems kind of brute force to find the key,
get the pos, increment by 2 (for the = and first ") then grab everything
up to the next ". But maybe this is the best approach?

Is there a pretty way to have a small function that does this:

void GetValFromKey(std::string& input, std::string& key, std::string&
value)
{
std::string val;
// find val from key
value = val;
}
Nov 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Bryan wrote:
If I have the following string from a huge xml file:

std::string s = "<input key1=\"v1\" key2=\"val4\" key3=\"test\" />";

I need to get the values associated with the keys out from this line.
What is the best way to do this? I was looking at sscanf, and
std::string find, but this seems kind of brute force to find the key,
get the pos, increment by 2 (for the = and first ") then grab everything
up to the next ". But maybe this is the best approach?

Is there a pretty way to have a small function that does this:

void GetValFromKey(std::string& input, std::string& key, std::string&
value)
{
std::string val;
// find val from key
value = val;
}
I'm thinking that parsing by value is not viable. You're probably
better off collecting the values in a container. The changing length of
each value as well as the number of values present should not cause the
function to fail.

#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <iterator>

template< typename S >
void parsekeys( const S& r_s, std::vector< S >& r_v)
{
typename S::size_type left(0), right(0);
while( r_s.npos (left = r_s.find_first_of('\"', right + 1)) )
{
right = r_s.find_first_of('\"', ++left);
r_v.push_back( S(r_s, left, right - left) );
}
}

int main()
{
std::string s = "<input key1=\"v1\" key2=\"val4\" key3=\"test\" />";
std::vector<std::stringvs;
parsekeys(s, vs);
std::copy( vs.begin(),
vs.end(),
std::ostream_iterator<std::string>( std::cout, "\n"
) );
return 0;
}

Another option is to load the keys and their corresponding values in a
std::map.
Have you considered Xerces?
http://xml.apache.org/
ot TinyXML?
http://www.grinninglizard.com/tinyxml/

Nov 3 '06 #2

P: n/a
VJ
Bryan wrote:
If I have the following string from a huge xml file:

std::string s = "<input key1=\"v1\" key2=\"val4\" key3=\"test\" />";

I need to get the values associated with the keys out from this line.
What is the best way to do this? I was looking at sscanf, and
std::string find, but this seems kind of brute force to find the key,
get the pos, increment by 2 (for the = and first ") then grab everything
up to the next ". But maybe this is the best approach?

Is there a pretty way to have a small function that does this:

void GetValFromKey(std::string& input, std::string& key, std::string&
value)
{
std::string val;
// find val from key
value = val;
}
We are using this library for our project to parse XML files :

http://iridia.ulb.ac.be/~fvandenb/tools/xmlParser.html

The library is very small, and easy to use.

The output from functions are strings, and you need to convert if you
need another type
Nov 3 '06 #3

P: n/a
>
Another option is to load the keys and their corresponding values in a
std::map.
Have you considered Xerces?
http://xml.apache.org/
ot TinyXML?
http://www.grinninglizard.com/tinyxml/
Thanks for the suggestion, I will give this a try...

We actually use Xerces now- the problem is that to extract info you need
to load in the entire file, which in our case is pretty large. I only
need one line which is the 5th line from the top of the file for some
information, so its faster (by far) to just parse this one line out for
the data.

Thanks!
Nov 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.