使用tinyXML2 读写配置文件

来源:互联网 发布:post请求返回json数据 编辑:程序博客网 时间:2024/06/06 16:30

1. 前言

我们上次写了一段用来提取图片中交点信息的程序: http://blog.csdn.net/zhyh1435589631/article/details/53084795
但是, 我们发现代码中存在好多奇异值,非常的讨人厌, 我们就希望可以讲这部分变化的参数移动到配置文件中, 我们只需要让程序读取这个配置文件中的参数就可以了。

2. TinyXML2

2.1 TinyXML2 基本介绍

  1. TinyXML2 的官网地址: http://www.grinninglizard.com/tinyxml2docs/index.html
  2. github 地址: https://github.com/leethomason/tinyxml2
  3. 这是一个非常简单的用来读写xml 文件的一个C++开源工具

2.2 配置文件实例

参考文章: http://blog.csdn.net/k346k346/article/details/44258739
我们精选一下我们的配置文件

<config>    <autoSet>        <autoSetGray2BinaryLower>true</autoSetGray2BinaryLower>        <CurrentMean>25</CurrentMean>    </autoSet></config>

2.3 读取配置文件

  1. 绑定配置文件:

    tinyxml2::XMLDocument doc;doc.LoadFile(filename.c_str());
  2. 读取配置文件信息
    使用FirstChildElement 和 FirstChild 组合即可

    bool CXML::getAutoSetGray2BinaryLower(){    string data = doc.FirstChildElement("config")        ->FirstChildElement("autoSet")        ->FirstChildElement("autoSetGray2BinaryLower")        ->FirstChild()->ToText()->Value();    return data == "true";}double CXML::getAutoSetCurrentMean(){    string data = doc.FirstChildElement("config")        ->FirstChildElement("autoSet")        ->FirstChildElement("CurrentMean")        ->FirstChild()->ToText()->Value();    return stod(data);}   

2.4 写配置文件

使用setText 写数据到相应节点中去

bool CXML::setAutoSetCurrentMean(double value){        tinyxml2::XMLElement * data = doc.FirstChildElement("config")            ->FirstChildElement("autoSet")            ->FirstChildElement("CurrentMean");        data->SetText(value);        return true;    }

3. 加入工程文件

项目工程地址: https://code.csdn.net/zhyh1435589631/picture_handle_tianwentai/tree/master
相应的配置文件 xml, 通过修改这个文件中的参数, 我们可以非常方便的控制图像处理过程的控制~~

<config>    <loadImage>        <show>false</show>    </loadImage>    <downSample>        <downSampleFactor_x>0.1</downSampleFactor_x>        <downSampleFactor_y>0.1</downSampleFactor_y>        <show>false</show>    </downSample>    <autoSet>        <autoSetGray2BinaryLower>true</autoSetGray2BinaryLower>        <CurrentMean>25</CurrentMean>    </autoSet>    <DealWithImageRough>        <show>false</show>        <Gray2Binary>            <lower>120</lower>            <higher>255</higher>        </Gray2Binary>        <EdgeDetection>            <canny_lower>50</canny_lower>            <canny_higher>200</canny_higher>        </EdgeDetection>        <DetectLines>            <rho>1</rho>            <theta_splitNums>180</theta_splitNums>            <threshold>80</threshold>            <minLineLength>70</minLineLength>            <maxLineGap>50</maxLineGap>        </DetectLines>        <RemoveDuplicateLines>            <threshold>70</threshold>            <angle_splitNum>24</angle_splitNum>        </RemoveDuplicateLines>        <CalcPoints>        </CalcPoints>        <RemoveDuplicatePoints>        </RemoveDuplicatePoints>    </DealWithImageRough>    <DealWithImageROI>        <ROI_size>300</ROI_size>        <show>false</show>        <Gray2Binary>            <lower>100</lower>            <higher>255</higher>        </Gray2Binary>        <EdgeDetection>            <canny_lower>50</canny_lower>            <canny_higher>200</canny_higher>        </EdgeDetection>        <DetectLines>            <rho>1</rho>            <theta_splitNums>180</theta_splitNums>            <threshold>80</threshold>            <minLineLength>200</minLineLength>            <maxLineGap>50</maxLineGap>        </DetectLines>        <RemoveDuplicateLines>            <threshold>70</threshold>            <angle_splitNum>24</angle_splitNum>        </RemoveDuplicateLines>        <CalcPoints>        </CalcPoints>        <RemoveDuplicatePoints>        </RemoveDuplicatePoints>    </DealWithImageROI></config>
0 0
原创粉丝点击