下面是一个使用boost.property_tree来解析XML/INI文件的简单示例。 使用boost.property_tree来作为配置文件的解析工具非常合适. #include <iostream> #include <string> #include <algorithm> #include <iterator> #include <set> #include <stdlib.h> #include <unistd.h> #include <boost/foreach.hpp> #include <boost/noncopyable.hpp> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> #include <boost/property_tree/ini_parser.hpp> int main( int argc , char *argv[] ) { std::string strConfigName( "" ); std::string strReportName( "" ); const char *szShortOptions = ":f:t:h"; while ( true ) { const int iOption = getopt( argc , argv , szShortOptions ); if ( -1 == iOption ) { break; } switch ( iOption ) { case 'f': { strConfigName = optarg; } break; case 't': { strReportName = optarg; } break; case ':': { std::cerr << "-" << static_cast< char >( iOption ) / << " need option value." << std::endl; } break; case 'h': { std::cout << "usage: " << argv[ 0 ] << " -f config [-h]" << std::endl; std::cout << "-f config specify the config file name." / << std::endl; std::cout << "-t report specify the target report name." / << std::endl; std::cout << "-h show the help information." / << std::endl; } return EXIT_SUCCESS; case '?': default: { std::cerr << "unknown option: " << static_cast< char >( iOption ) / << std::endl; } break; } } if ( ( true == strConfigName.empty() ) / || ( true == strReportName.empty() ) ) { std::cout << "ERROR, usage: " << argv[ 0 ] / << " -f config" / << "/t" / << " -t report" << std::endl; return -1; } /* typedef ::boost::property_tree::ptree IniParser; IniParser iniParser; const std::string strNullString = ""; const int iDefaultIntValue = 0; read_ini( strConfigName , iniParser ); const std::string strLogName = iniParser.get( "debug.log_name" , strNullString ); const int iLogLevel = iniParser.get( "debug.level" , iDefaultIntValue ); typedef std::set< std::string > ModuleSet; ModuleSet moduleSet; BOOST_FOREACH( IniParser::value_type &node , / iniParser.get_child( "modules" ) ) { moduleSet.insert( node.second.data() ); } std::string strIndex( "1" ); IniParser reportINI; reportINI.put( "debug.log_name" , strLogName ); reportINI.put( "debug.level" , iLogLevel); BOOST_FOREACH( const std::string &strName , moduleSet ) { reportINI.put( "modules.module" + strIndex , / strName ); strIndex[ 0 ] += 1; } write_ini( strReportName , reportINI ); */ typedef ::boost::property_tree::ptree XmlParser; XmlParser xmlParser; const std::string strNullString = ""; const int iDefaultIntValue = 0; read_xml( strConfigName , xmlParser ); const std::string strLogName = xmlParser.get( "debug.log_name" , strNullString ); const int iLogLevel = xmlParser.get( "debug.level" , iDefaultIntValue ); typedef std::set< std::string > ModuleSet; ModuleSet moduleSet; BOOST_FOREACH( XmlParser::value_type &node , / xmlParser.get_child( "debug.modules" ) ) { moduleSet.insert( node.second.data() ); } XmlParser reportXml; reportXml.put( "debug.log_name" , strLogName ); reportXml.put( "debug.level" , iLogLevel); BOOST_FOREACH( const std::string &strName , moduleSet ) { reportXml.put( "debug.modules.module" , / strName ); } write_xml( strReportName , reportXml ); return EXIT_SUCCESS; }