boost解析XML方法教程

6 篇文章 0 订阅


boost库在解析XML时具有良好的性能,可操作性也很强下地址有个简单的说明

http://blog.csdn.net/luopeiyuan1990/article/details/9445691

一下是做的两个实例:入门教程,大神见笑,希望能帮助你尽快进入XML的开发之旅。

<root>
 <delfile>

  <filenum> 35 </filenum>

  <paths>
   <path> 
    <pathname>/tmp/tmp0/</pathname>
    <before_hours> 0 </before_hours>
   </path>
  
   <path> 
    <pathname>/tmp/tmp1/</pathname>
    <before_hours> 1 </before_hours>
   </path>
  
   <path> 
    <pathname>/tmp/tmp2/</pathname>
    <before_hours> 2 </before_hours>
   </path>
  
   <path> 
    <pathname>/tmp/tmp3/</pathname>
    <before_hours> 3 </before_hours>
   </path>
  
   <path> 
    <pathname>/tmp/tmp4/</pathname>
    <before_hours> 4 </before_hours>
   </path>
  </paths>

 </delfile>


 <backup>
  <backuptime> 23:59 </backuptime>
 </backup>

</root>
代码:(例程引用了狂人山庄的博客,感谢网友)

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/typeof/typeof.hpp>
#include <iostream>

using namespace std;

/*       相关的xml参考文件请参考del.conf           */

void ReadConfig()
{
  	boost::property_tree::ptree pt;   //定义一个存放xml的容器指针
	boost::property_tree::read_xml("del.conf", pt);   //读入目录下 del.conf文件 入口在pt这个指针
	int filenum = pt.get<int>("root.delfile.filenum"); //将 xml文件中, root节点,下一层delfile 下一层的filenum 作为int类型取出,存在在filenum变量中。

	cout << "filenum: " << filenum << endl;//不注释了 - -

	BOOST_AUTO(child, pt.get_child("root.delfile.paths")); //BOOST_AUTO自动获取表达式, 这里定义个一个节点child指针,并将指针指向 root下一层的delfile的下一层的paths
	for (BOOST_AUTO(pos, child.begin()); pos != child.end(); ++pos)//由于paths节点有多个节点,并且这些节点名称一样,可以用遍历的方法来获取他们,方法见左
	{
		 BOOST_AUTO(child_paths, pos->second.get_child("")); //此处不需要填结点名,但引号不能省.这里是获取该节点下所有子节点的意思,子节点获取后放在child_path这个指针
		 for (BOOST_AUTO(pos_paths, child_paths.begin()); pos_paths != child_paths.end(); ++pos_paths)
		 	 cout << pos_paths->second.data() << endl;
	}
        
}
int main()
{
 ReadConfig();
 return 0;
}

第二个教程。代码可读性不是很高,但是写法用了一个FOREACH,很简洁。

<debug name="debugname">  
    <file name="debug.log"/>  
    <modules type="internal">  
        <module1>Finance_Internal</module1>  
        <module2>Admin_Internal</module2>  
        <module3>HR_Internal</module3>  
    </modules>  
      
    <modules type="external">  
        <module>Finance_External1</module>  
        <module>Admin_External2</module>  
        <module>HR_External3</module>    
    </modules>  
</debug>  

相应代码:

/*
 * test2.cpp
 *
 *  Created on: Jul 23, 2013
 *      Author: leo_luopy
 */
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>

using namespace std;
using namespace boost::property_tree;

int main(void)
{
	ptree pt;  //定义一个容器入口指针
	read_xml("debug_settings.xml", pt);


	BOOST_FOREACH(ptree::value_type &v1, pt.get_child("debug")){

		if(v1.first == "<xmlattr>"){ //it's an attribute
			//read debug name="debugname"
			cout<< "debug name=" << v1.second.get<string>("name") << endl;
		}else if(v1.first == "file"){
			//read file name="debug.log"
			cout << "  file name=" << v1.second.get<string>("<xmlattr>.name") << endl;
		}
		else{ // v1.first == "modules"
			//get module type
			cout<< "  module type:" << v1.second.get<string>("<xmlattr>.type") << endl;

			//loop for every node under modules
			BOOST_FOREACH(ptree::value_type &v2, v1.second){
				if(v2.first == "<xmlattr>"){  //it's an attribute
					//this can also get module type
					cout<< "  module type again:" << v2.second.get<string>("type") << endl;
				}
				else{
					//all the modules have the same structure, so just use data() function.
					cout<< "    module name:" << v2.second.data() << endl;
				}
			}//end BOOST_FOREACH
		}
	}//end BOOST_FOREACH
	return 0 ;
}


















  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要修改一个 XML 文件中的某一项,可以使用 Boost XML 库提供的 API 来实现: 1. 首先,需要读取 XML 文件,并将其解析XML 对象,可以使用 `boost::property_tree::xml_parser::read_xml` 函数来实现。 ```c++ boost::property_tree::ptree pt; boost::property_tree::xml_parser::read_xml("example.xml", pt); ``` 2. 然后,需要找到需要修改的 XML 节点,可以使用 `boost::property_tree::ptree::get_child` 函数获取指定节点的子节点。 ```c++ boost::property_tree::ptree& node = pt.get_child("root.node_to_modify"); ``` 3. 接下来,可以通过修改节点的值来对其进行修改。 ```c++ node.put_value("new_value"); ``` 4. 最后,需要将修改后的 XML 对象重新写入文件,可以使用 `boost::property_tree::xml_parser::write_xml` 函数来实现。 ```c++ boost::property_tree::xml_parser::write_xml("example.xml", pt); ``` 完整的代码如下: ```c++ #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> #include <iostream> int main() { boost::property_tree::ptree pt; boost::property_tree::xml_parser::read_xml("example.xml", pt); boost::property_tree::ptree& node = pt.get_child("root.node_to_modify"); node.put_value("new_value"); boost::property_tree::xml_parser::write_xml("example.xml", pt); return 0; } ``` 注意,以上代码只是修改了内存中的 XML 对象,并没有直接修改文件。如果需要直接修改文件,可以在写入文件之前先将原文件备份,然后再写入修改后的 XML 对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值