使用boost讀取XML檔案

2021-06-14 17:59:09 字數 933 閱讀 1305

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表示支援寬字元。

例如:config.xml

<?xml version="1.0" encoding="utf-8"?>

1fansy

我要讀取它的資料:

#include #include #include #include using namespace std;

using namespace boost::property_tree;

int main()

{ ptree pt;

read_xml("conf.xml",pt); //讀入乙個xml檔案

cout<<"id is "<("con.id")<("con.no_prop",100)<("name")config.xml為:

<?xml version="1.0" encoding="utf-8"?>

1sword

使用Boost讀取xml

boost中提供了對配置檔案讀取的支援,它就是 property tree。basic ptree 是property tree的核心基礎。其介面像std list。可以執行很多基本的元素操作,比如使用begin end 等。此外還加入了操作屬性樹的get get child get value d...

使用minidom讀取xml檔案

測試xml 樹中每乙個節點有三種基本屬性 根名.nodename為節點標籤的名字。比如標籤的nodename為aa 根名.nodevalue是節點的值,只對文字結點有效。根名.nodetype是節點的型別 每乙個節點都有它的 nodename nodevalue nodetype屬性對於知道元素名字...

讀取XML檔案

private void button2 click object sender,eventargs e using filestream fs new filestream filename,filemode.open,fileaccess.read n xl.name 是否有屬性 if xl.h...