Reading an XML file using boost

Source: Internet
Author: User


Support for profile reading is provided in Boost, which is: Property_tree.

Basic_ptree is the core foundation of Property_tree. Its interface is like Std::list. You can perform many basic element operations, such as Begin (), End (), and so on.

Additional operations such as Get (), Get_child (), Get_value (), data () are added to the Operation property tree.
Basic_ptree has two important internal definitions, self_type and value_type. Self_type is the type of the Basic_ptree template itself after instantiation, which is also the type of the child node. Value_type is the data structure of a node, which is an std::p air, which contains the property name (first) and the node itself (second).
The basic_ptree is usually not used, but a predefined typedef is used. Ptree, Wptree, Iptree, Wiptree. The prefix I indicates that the case is ignored, and the prefix W indicates support for wide characters.
For example: CONFIG.

<?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 in 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 of Ptree child = Pt.get_child ("con") is used;    Take a child node cout<< "name is:" <<child.get<string> ("name") <<endl;
    Sub-node operation, in fact, as above the operation of child = Pt.get_child ("Con.urls"); For (Boost_auto (Pos,child.begin ());p OS! = Child.end (); ++pos)//boost in 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 operation is displayed as:
ID is 1
Try Default100
name Is:fansy
        http://blog.csdn.net//fansongy
        http://weibo.com// Fansongy

Config:
<?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> 

this blog from the Asura Road, reproduced please indicate the source: http://blog.csdn.net/fansongy/article/details/9026407


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.