需求:欲讀取下列XML檔案中的TEXT資料:
127.0.0.1 800
string readXmlSingleText(string file,string xpath){ char errStr[MSG_MIN_LEN] = {'\0'}; TiXmlDocument xmlDoc(file.c_str()); xmlDoc.LoadFile(); if(xmlDoc.ErrorId() > 0){ //error do something.. } TiXmlElement* pRootElement = xmlDoc.RootElement(); if(!pRootElement){ //error do something.. } TinyXPath::xpath_processor xproc(pRootElement,xpath.c_str()); unsigned nodeCount =xproc.u_compute_xpath_node_set(); if(nodeCount==0) { //error do something.. } TiXmlNode *xnode = xproc.XNp_get_xpath_node(0); return string(xnode->ToElement()->GetText()); }
使用方式如下:
string ip = readXmlSingleText("setting.xml","//ROOT/SEND-IP");
沒有留言:
張貼留言