473,385 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Parsing question

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
3 1379

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
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
>
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Todd Moyer | last post by:
I would like to use Python to parse a *python-like* data description language. That is, it would have it's own keywords, but would have a syntax like Python. For instance: Ob1 ('A'): Ob2...
4
by: silviu | last post by:
I have the following XML string that I want to parse using the SAX parser. If I remove the portion of the XML string between the <audit> and </audit> tags the SAX is parsing correctly. Otherwise...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
6
by: Ulrich Vollenbruch | last post by:
Hi all! since I'am used to work with matlab for a long time and now have to work with c/c++, I have again some problems with the usage of strings, pointers and arrays. So please excuse my basic...
13
by: 31337one | last post by:
Hello everyone, I am writing an application that uses a command line interface. It will be configurable by passing arguments on the command line. The program is going to run in windows and...
5
by: bmichel | last post by:
Hey, What I'm doing is the following: - Load XML data a file - Parsing the XML data - Printing some parsed content The problem is that the script execution is stopping before all the...
6
by: jackwootton | last post by:
Hello everyone, I understand that XML can be parsed using JavaScript using the XML Document object. However, it is possible to parse XHTML using JavaScript? I currently listen for DOMMutation...
0
by: Ole Nielsby | last post by:
(sorry, wrong button, here is the real post:) I'm working on a C++ parser which is to be used for various code analysis and transformation tools. (It's part of my PILS programming system which...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
3
by: dimasteg | last post by:
Hi all C. Nead some help with string "on the fly" parsing, how it can be realized ? Any ideas? I got some of my own, but it's interesting to get other points of view . Regards.
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.