使用boost库解析xml配置文件

       Boost PropertyTree 所提供的 XML Parser 基本上是基于一个小巧快速的开源XML解析器RapidXML 这个 OpenSource 的 XML parser 来的;而官方文件裡也有提到,他并没有完整地支援 XML 所有的标准(例如他无法处理 DTD、也不支援 encoding 的处理),这是在使用上要注意的地方。不过对于一般使用来说,基本上应该是够用了。

       boost中提供了对配置文件读取的支持,它就是:property_tree。

       basic_ptree 是property_tree的核心基础。其接口像std::list。可以执行很多基本的元素操作,比如使用begin()、end()等。此外还加入了操作属性树的get()、get_child()、get_value()、data()等额外的操作。

       basic_ptree有两个重要的内部定义self_type和value_type。self_type是basic_ptree模板实例化后自身的类型,它也是子节点的类型。value_type是节点的数据结构,它是一个std::pair,它含有属性名(first)和节点自身(second)。
      通常不使用basic_ptree,而是使用预定义的typedef。ptree、wptree、iptree、wiptree。前缀i表示忽略大小写,前缀w表示支持宽字符。
      今天由于工作上要读取带属性并且有几层嵌套的xml配置文件,因此研究一下如何使用。下面直接列出测试用的xml配置文件和程序代码。
 
测试的xml配置文件:set.xml
<root>
     <Capture fillter="tcp"/>
     <Analysis>   
           <soinfo desc="analysis1" path="/rootMysqlCaptureweibo.so1" version="123"/>
           <soinfo desc="analysis4" path="/rootMysqlCaptureweibo.so2" version="456"/>
           <soinfo desc="analysis7" path="/rootMysqlCaptureweibo.so3" version="789"/>
    </Analysis>
    <Output>
         <rule>    
                <cond key="1" value="2" relation="3"/>
                <cond key="4" value="5" relation="6"/>
                 <cond key="7" value="8" relation="9"/>
            <out where="file" dir="/root" dbname="" table=""/>   
        </rule>
        <rule>
               <cond key="11" value="12" relation="13"/>
               <cond key="14" value="15" relation="16"/>
               <cond key="17" value="18" relation="19"/>
            <out where="oracle" dir="" dbname="/root/home" table=""/>
        </rule>
        <rule>
              <cond key="21" value="22" relation="23"/>
              <cond key="24" value="25" relation="26"/>
              <cond key="27" value="28" relation="29"/>
           <out where="mysql" dir="" dbname="" table="/root/home/liu"/>
        </rule>
   </Output>
</root>
 
 
测试程序:

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/typeof/typeof.hpp> 
#include <boost/foreach.hpp>
#include <string>
#include <vector>
#include <exception>


std::string CaptureFillter;

struct AnalysisSoInfo {
    std::string desc;
    std::string path;
    std::string version;
};
typedef std::vector<AnalysisSoInfo> AnalysisSetting;

struct AtomCondition {
    std::string key;
    std::string value;
    std::string relation;
};
typedef std::vector<AtomCondition> OutputCondition;

struct OutputWhere {
    std::string _where; 
    std::string _dir;
    std::string _dbname;
    std::string _table;
};


struct OutputRule {
    OutputCondition _outputCondition;
    OutputWhere _outputWhere;
};
typedef std::vector<OutputRule> OutPutSetting;


void fun(std::vector<OutputRule> v_output) {
    std::cout << "size" << v_output.size() << std::endl;
    std::vector<OutputRule>::iterator itr;
    for (itr = v_output.begin(); itr != v_output.end(); itr++) {
        std::cout <<"where=" <<itr->_outputWhere._where ;
        std::cout <<"  dir="<< itr->_outputWhere._dir ;
        std::cout <<"  dbname="<< itr->_outputWhere._dbname ;
        std::cout <<"  table="<< itr->_outputWhere._table <<std::endl;
        
        OutputCondition::iterator itrout;
        for (itrout = itr->_outputCondition.begin(); itrout != itr->_outputCondition.end(); itrout++) {
            std::cout <<"key="<< itrout->key ;
            std::cout <<"  value=" <<itrout->value ;
            std::cout <<"  relation="<< itrout->relation << std::endl;
        }
    }
}


int main() {
    using namespace boost::property_tree;
    char szXmlFile[] = "set.xml";

    ptree pt;
    read_xml(szXmlFile, pt); // 读取xml文件,并转成property tree
    ptree& root = pt.get_child("root"); //定位到root节点

    ptree& capture = pt.get_child("root.Capture"); //定位到Capture节点
    CaptureFillter = capture.get< std::string>("<xmlattr>.fillter");
    std::cout << "  Capture fillter =" << CaptureFillter << std::endl;

    ptree& analysis = pt.get_child("root.Analysis"); //定位到Analysis节点
AnalysisSetting v_analysissetting;
AnalysisSoInfo temp_soinfo;
    BOOST_FOREACH(ptree::value_type &v1, analysis) //遍历analysis节点
    {
        if (v1.first == "soinfo")
        {
            temp_soinfo.desc.clear();
            temp_soinfo.path.clear();
            temp_soinfo.version.clear();
            temp_soinfo.desc = v1.second.get<std::string>("<xmlattr>.desc");
            temp_soinfo.path = v1.second.get<std::string>("<xmlattr>.path");
            temp_soinfo.version = v1.second.get<std::string>("<xmlattr>.version");
            if ((temp_soinfo.desc.empty()) || (temp_soinfo.path.empty()) || (temp_soinfo.version.empty())) {
                std::cout << "one of the temp_soinfo is empty" << std::endl;
                return -1;
            }
        }
        v_analysissetting.push_back(temp_soinfo);
        std::cout << "desc=" << temp_soinfo.desc;
        std::cout << "  path=" << temp_soinfo.path;
        std::cout << "  version=" << temp_soinfo.version << std::endl;
    }

    ptree& output = pt.get_child("root.Output"); 定位到Output节点
OutPutSetting v_outputsetting;
AtomCondition temp_condition;
    OutputWhere temp_where;
    OutputRule temp_rule;
    for (ptree::iterator itr = output.begin(); itr != output.end(); itr++) {
        OutputCondition v_outputcondition; //一个rule节点,一个vector

        ptree rule = itr->second;
        BOOST_FOREACH(ptree::value_type &v2, rule) 
        {
            if (v2.first == "cond") {
                temp_condition.key.clear();  
                temp_condition.value.clear();
                temp_condition.relation.clear();
                temp_condition.key = v2.second.get<std::string>("<xmlattr>.key");
                temp_condition.value = v2.second.get<std::string>("<xmlattr>.value");
                temp_condition.relation = v2.second.get<std::string>("<xmlattr>.relation");
                if ((temp_condition.key.empty()) || (temp_condition.value.empty()) || (temp_condition.relation.empty())) {
                    std::cout << "one of the temp_condition is empty" << std::endl;
                    return -1;
                } //此处判空,未获取到,则退出  
                v_outputcondition.push_back(temp_condition);
            }
            if (v2.first == "out") {
                temp_where._where.clear();
                temp_where._dir.clear();
                temp_where._dbname.clear();
                temp_where._table.clear();
                temp_where._where = v2.second.get<std::string>("<xmlattr>.where");
                temp_where._dbname = v2.second.get<std::string>("<xmlattr>.dbname");
                temp_where._dir = v2.second.get<std::string>("<xmlattr>.dir");
                temp_where._table = v2.second.get<std::string>("<xmlattr>.table");
                if (temp_where._where.empty()) {
                    std::cout << "temp_where._where is empty" << std::endl;
                    return -1;
                }
                if (temp_where._dbname.empty() && temp_where._dir.empty() && temp_where._table.empty()) {
                    std::cout << "the all of temp_where is empty" << std::endl;
                    return -1;
                }
                temp_rule._outputWhere = temp_where;
            }
        }
        temp_rule._outputCondition = v_outputcondition;
        v_outputsetting.push_back(temp_rule);
    }
    fun(v_outputsetting);
}




 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以通过以下步骤下载和配置Boost在VSCode中使用: 1. 首先,从Boost官方网站(https://www.boost.org/)下载最新版本的Boost。下载的文件通常是一个压缩文件,比如boost_1_72_0.tar.gz。 2. 将下载的Boost文件解压缩到您的计算机上的任意位置。您可以选择一个方便的目录,比如将其解压缩到您的用户文件夹中。 3. 打开VSCode,并在左侧的资源管理器中选择您的Boost项目文件夹。 4. 在VSCode的顶部菜单中选择“终端(Terminal)”>“新建终端(New Terminal)”以打开终端。 5. 在终端中,输入以下命令来安装CMake工具: ``` sudo apt-get install cmake ``` 注意:如果您的操作系统不是Ubuntu,可能需要使用适合您操作系统的相应命令来安装CMake。 6. 在终端中,输入以下命令来创建一个构建目录并进入该目录: ``` mkdir build cd build ``` 7. 在构建目录中,运行以下命令来配置项目: ``` cmake .. ``` 这将使用CMake来自动查找并配置Boost。 8. 配置完成后,运行以下命令来构建项目: ``` cmake --build . ``` 这将编译您的项目,并生成可执行文件。 9. 完成后,您可以在构建目录中找到生成的可执行文件。您可以使用以下命令运行它: ``` ./boost_demo ``` 这将运行您的Boost项目,并输出结果。 通过以上步骤,您就可以成功下载并配置Boost在VSCode中使用了。请注意,这只是一个简单的示例,您可能需要根据您的实际情况进行一些额外的配置和设置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [boost在visual studio、DevC++和vscode上的环境配置](https://blog.csdn.net/qq_41854911/article/details/118440683)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [VSCode+CMake+MSYS2开发环境中使用boost](https://blog.csdn.net/falwat/article/details/124778625)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值