bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

boost解析xml文件(讀取)-創新互聯

昨天做了個解析xml文件的接口,百度了boost的接口,再結合自己踩過的坑,特做此記錄。
先上代碼,看看如何獲取到xml文件節點屬性和內容:
xml文件如下:

創新互聯長期為上千余家客戶提供的網站建設服務,團隊從業經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯網生態環境。為東安企業提供專業的成都網站制作、網站設計,東安網站改版等技術服務。擁有10年豐富建站經驗和眾多成功案例,為您定制開發。
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;
}

運行結果如下:

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"

大的坑:
在遍歷子節點時,如果父節點本身帶有屬性,會多出一個的子節點,所以要判斷一下子節點的first是否等于子節點的名稱(第二個坑:
不用懷疑,當獲取某個節點的內容時,由于xml的縮進換行美化效果,會造成內容前面或后面會有回車換行,甚至可能由于手動編輯過,會有我們看不到的制表符/t或空格,所以在獲取內容時,往往需要去除兩端空白。
第三個坑:
獲取xml的某個節點的屬性xmlattr時,如果該屬性不存在,會拋異常,所以建議使用時給默認值可以避免拋出異常,如果屬性必須存在,可以用try語句。

下面演示獲取具體某個節點:

#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 + "節點"<(".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;
}

你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧

名稱欄目:boost解析xml文件(讀取)-創新互聯
文章地址:http://vcdvsql.cn/article18/cedhgp.html

成都網站建設公司_創新互聯,為您提供軟件開發標簽優化外貿網站建設網站建設網站制作App設計

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

手機網站建設