Use boost to read XML files detailing _c language

Source: Internet
Author: User

Boost reads XML file

Boost provides support for configuration file reads: Property_tree.

Basic_ptree is the core foundation of Property_tree. Its interface is like Std::list. You can perform a number of basic element operations, such as using begin (), End (), and so on.
Additional operations such as Get (), Get_child (), Get_value (), data () of the operation attribute tree are also added.

Basic_ptree has two important internal definitions of self_type and value_type. Self_type is the type of the Basic_ptree template after it is instantiated, and it is also a child node type. Value_type is the data structure of a node, which is an std::p air, which contains the property name (a) and the node itself (second).

Typically, instead of using Basic_ptree, you use a predefined typedef. Ptree, Wptree, Iptree, Wiptree. Prefix I indicates that case is ignored, and prefix w indicates support for wide characters.

For example:

Config.xml

<?xml version= "1.0" encoding= "Utf-8"?> 
<con> 
 <id>1</id> 
 <name>fansy </name> 
 <urls> 
  <url>http://blog.csdn.net//fansongy</url> 
  <url>http:/ /weibo.com//fansongy</url> 
 </urls> 
</con>  

I want to read its data:



#include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_ 
Parser.hpp> #include <boost/typeof/typeof.hpp> using namespace std; 
Using namespace boost::p roperty_tree; 
  int main () {Ptree pt;   Read_xml ("Conf.xml", PT); Read an XML file cout<< "ID is" <<pt.get<int> ("Con.id") <<endl; Read the information in the node cout<< "Try Default" <<pt.get<int> ("Con.no_prop" <<endl;  If not, the default value is used Ptree child = Pt.get_child ("con");  Take a child node cout<< "name is:" <<child.get<string> ("name") <<endl; 
  Child node operation, in fact, as the above operation child = Pt.get_child ("Con.urls"); For (Boost_auto (Pos,child.begin ());p OS!= child.end () ++pos)//boost AUTO {cout<< "\ T" +pos->second.data 
   () <<endl;  } pt.put ("Con.name", "sword"); Change a key value Pt.add ("Con.urls.url", http://www.baidu.com);  Add a key value Write_xml ("Conf.xml", PT); 
  Writes XML GetChar (); 
return 0; 

 }


The display is run as: 
ID is 1 
Try Default100 
name Is:fansy 
    http://blog.csdn.net//fansongy 
    http://weibo.com// Fansongy 



Config.xml: 
<?xml version= "1.0" encoding= "Utf-8"?> 
<con> 
 <id>1</id> 
 <name>Sword</name> 
<urls> 
   <url>http://blog.csdn.net//fansongy</url> 
   <url>http://weibo.com//fansongy</url> 
   <url>http://www.baidu.com</url> 
</urls> 
</con>  



Thank you for reading, I hope to help you, thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.