0

I have problem with boost::regex, this solution works only for one result in each match

boost::regex regex("id=\"(.*?)\""); // should I use this "id=\"(.*?)\"(.*?)<value>(.*?)</value>"?
boost::sregex_token_iterator iter(xml.begin(), xml.end(), regex, 1); // 1 because I just need text inside quotes
boost::sregex_token_iterator end;

and now parsed string

<x id="first">
<value>5</value>
</x>
<x id="second"> 
<value>56</value>  
</x>  
etc... 

Now question is how to parse id and value at once to grab them both inside matches loop

for( ; iter != end; ++iter ) {
  std::string id(iter->first, iter->second);
  std::string value(?????);
}
ildjarn
  • 62,044
  • 9
  • 127
  • 211
user1112008
  • 432
  • 10
  • 27

1 Answers1

1

Boost.PropertyTree contains a XML parser that you can use instead of regexes:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
...    
using boost::property_tree::ptree;
ptree pt;
read_xml(istreamOrFilename, pt);
BOOST_FOREACH(ptree::value_type &v, pt) {
    std::string id(v.second.get<std::string>("<xmlattr>.id"));
    std::string value(v.second.get<std::string>("value").data());    
}
alexisdm
  • 29,448
  • 6
  • 64
  • 99