昨天做了個解析xml文件的接口,百度了boost的接口,再結(jié)合自己踩過的坑,特做此記錄。
先上代碼,看看如何獲取到xml文件節(jié)點屬性和內(nèi)容:
xml文件如下:
data1
12 "jack" "0001"
hello2
13 "toni" "0002"
disk 333
14 "hate" "0003"
代碼如下:
#includeusing namespace std;
#include#includeusing namespace boost::property_tree;
string& trim(string &s)
{if (s.empty())
{return s;
}
string blanks("\f\v\r\t\n ");
s.erase(0, s.find_first_not_of(blanks));
s.erase(s.find_last_not_of(blanks) + 1);
return s;
}
int main(int argc, char **argv)
{ptree pt;
const char *filename = "t.xml";
read_xml(filename, pt);
const ptree &strages = pt.get_child("root");
for (auto it = strages.begin(); it != strages.end(); ++it)
{if (it->first != "strage") { cout<< it->first<< endl;
continue;
}
ptree strage = it->second;
string &data = strage.data();
data = trim(data);
string name = strage.get(".name");
int alias = strage.get(".alias");
float classId = strage.get(".classId");
printf("data: %s\nname: %s alias: %d classId: %f\n", data.c_str(), name.c_str(), alias, classId);
const ptree& idChild = strage.get_child("id");
const ptree& nameChild = strage.get_child("name");
const ptree& codeChild = strage.get_child("code");
const string& child_id = idChild.data();
const string& child_name = nameChild.data();
const string& child_code = codeChild.data();
printf("id: %s name: %s code: %s\n", child_id.c_str(), child_name.c_str(), child_code.c_str());
}
return 0;
}
運行結(jié)果如下:
data: data1
name: asd alias: 13 classId: 13.220000
id: 12 name: "jack" code: "0001"
data: hello2
name: abbasd alias: 14 classId: 14.220000
id: 13 name: "toni" code: "0002"
data: disk 333
name: assasd alias: 15 classId: 15.440000
id: 14 name: "hate" code: "0003"
大的坑:
在遍歷子節(jié)點時,如果父節(jié)點本身帶有屬性,會多出一個的子節(jié)點,所以要判斷一下子節(jié)點的first是否等于子節(jié)點的名稱(
不用懷疑,當(dāng)獲取某個節(jié)點的內(nèi)容時,由于xml的縮進(jìn)換行美化效果,會造成內(nèi)容前面或后面會有回車換行,甚至可能由于手動編輯過,會有我們看不到的制表符/t或空格,所以在獲取內(nèi)容時,往往需要去除兩端空白。
第三個坑:
獲取xml的某個節(jié)點的屬性xmlattr時,如果該屬性不存在,會拋異常,所以建議使用時給默認(rèn)值可以避免拋出異常,如果屬性必須存在,可以用try語句。
下面演示獲取具體某個節(jié)點:
#include#include#include#includeusing namespace boost::property_tree;
using namespace std;
ptree *get_node_by_attr(ptree &nodes, const string label, const string attr, const string value)
{for (ptree::iterator it = nodes.begin(); it != nodes.end(); it++)
{if (it->first != label)
{continue;
}
ptree &item = it->second;
string name = item.get(string(".") + attr, "");
if (name == value)
{return &item;
}
}
return NULL;
}
string& trim(string &s)
{if (s.empty())
{return s;
}
string blanks("\f\v\r\t\n ");
s.erase(0, s.find_first_not_of(blanks));
s.erase(s.find_last_not_of(blanks) + 1);
return s;
}
int main(int argc, char **argv)
{ptree pt;
xml_parser::read_xml(string("t.xml"), pt, xml_parser::no_comments);
ptree &child = pt.get_child("root");
const string label = "strage";
const string attr = "name";
const string value = "abbasd";
ptree *node = get_node_by_attr(child, label, attr, value);
if (NULL == node)
{cout<< "沒有name=" + attr + "的" + label + "節(jié)點"<(".name");
int alias = strage.get(".alias");
float classId = strage.get(".classId");
printf("data: %s\nname: %s alias: %d classId: %f\n", data.c_str(), name.c_str(), alias, classId);
const ptree& idChild = strage.get_child("id");
const ptree& nameChild = strage.get_child("name");
const ptree& codeChild = strage.get_child("code");
const string& child_id = idChild.data();
const string& child_name = nameChild.data();
const string& child_code = codeChild.data();
printf("id: %s name: %s code: %s\n", child_id.c_str(), child_name.c_str(), child_code.c_str());
return 0;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
名稱欄目:boost解析xml文件(讀?。?創(chuàng)新互聯(lián)
文章地址:http://m.rwnh.cn/article18/cedhgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、標(biāo)簽優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、網(wǎng)站制作、App設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容