rapidxml解析xml文档
生活随笔
收集整理的這篇文章主要介紹了
rapidxml解析xml文档
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
將三個.hpp文件放入工程源碼中編譯
示例xml文件
<?xml version="1.0" encoding="GB2312" ?> <server listenport="6002"><roadlist><road name="chenghuilu1" beginlongtidue="120.000001" beginlatitude="10.000000" endlongtidue="120.000002" endlatitude="10.000000"></road><road name="chenghuilu2" beginlongtidue="120.000001" beginlatitude="10.000000" endlongtidue="120.000002" endlatitude="10.000000"></road><road name="chenghuilu3" beginlongtidue="120.000001" beginlatitude="10.000000" endlongtidue="120.000002" endlatitude="10.000000"></road></roadlist><rfidlist><point ID="22-22-22" longtidue="120.000001" latitude="10.000000"></point><point ID="33-33-33" longtidue="120.000001" latitude="10.000000"></point><point ID="44-44-44" longtidue="120.000001" latitude="10.000000"></point><point ID="55-55-55" longtidue="120.000001" latitude="10.000000"></point></rfidlist> </server>解析方式
try{rapidxml::file<> fdoc("D:/work/baosteel-smartrail/iplatda_code/Cmake_project/bin/config/MulDateLocalCalcServer.xml");rapidxml::xml_document<> doc;doc.parse<0>(fdoc.data());xml_node<>* root = doc.first_node();m_nlistenPort = atoi(root->first_attribute("listenport")->value());xml_node<>* roadlistNode = root->first_node("roadlist");for (xml_node<>* oneFileItem = roadlistNode->first_node(); oneFileItem != nullptr; oneFileItem = oneFileItem->next_sibling()){if (!oneFileItem->first_attribute("name") || !oneFileItem->first_attribute("beginlongtidue") || !oneFileItem->first_attribute("beginlatitude")|| !oneFileItem->first_attribute("endlongtidue") || !oneFileItem->first_attribute("endlatitude")){continue;}string strRoadName = oneFileItem->first_attribute("name")->value();string strBeginlongtidue = oneFileItem->first_attribute("beginlongtidue")->value();string strBeginlatitude = oneFileItem->first_attribute("beginlatitude")->value();string strEndlongtidue = oneFileItem->first_attribute("endlongtidue")->value();string strEndlatitude = oneFileItem->first_attribute("endlatitude")->value();CVLog.LogMessage(LOG_LEVEL_NOTICE, "load road:name:%s x1:%s y1:%s x2:%s y2:%s", strRoadName.c_str(), strBeginlongtidue.c_str(), strBeginlatitude.c_str(), strEndlongtidue.c_str(), strEndlatitude.c_str());RoadInfo* road = new RoadInfo();road->strRoadName = strRoadName;road->dbBeginlongtidue = atof(strBeginlongtidue.c_str());road->dbBeginlatitude = atof(strBeginlatitude.c_str());road->dbEndlongtidue = atof(strEndlongtidue.c_str());road->dbEndlatitude = atof(strEndlatitude.c_str());BL2XY(road->dbBeginlongtidue, road->dbBeginlatitude, road->dbBeginX, road->dbBeginY);BL2XY(road->dbEndlongtidue, road->dbEndlatitude, road->dbEndX, road->dbEndY);m_map_road_info[strRoadName] = road;}xml_node<>* RFIDlistNode = root->first_node("rfidlist");for (xml_node<>* oneRFID = RFIDlistNode->first_node(); oneRFID != nullptr; oneRFID = oneRFID->next_sibling()){if (!oneRFID->first_attribute("ID") || !oneRFID->first_attribute("longtidue") || !oneRFID->first_attribute("latitude")){continue;}string strID = oneRFID->first_attribute("ID")->value();string strlongtidue = oneRFID->first_attribute("longtidue")->value();string strlatitude = oneRFID->first_attribute("latitude")->value();CVLog.LogMessage(LOG_LEVEL_NOTICE, "load RFID ID:%s longtidue:%s latitude:%s", strID.c_str(), strlongtidue.c_str(), strlatitude.c_str());RFIDInfo* RFID = new RFIDInfo();RFID->strID = strID;RFID->dblongtidue = atof(strlongtidue.c_str());RFID->dblatitude = atof(strlatitude.c_str());BL2XY(RFID->dblongtidue, RFID->dblatitude, RFID->dbX, RFID->dbY);m_map_RFID_info[strID] = RFID;}doc.clear();}catch (exception &e){CVLog.LogMessage(LOG_LEVEL_ERROR, "load xml error :%s", e.what());}示例xml文檔
<?xml version="1.0" encoding="GB2312" ?> <Server><CommServerIP>127.0.0.1</CommServerIP> <CommServerPort>6000</CommServerPort> <HIMCalcServerIP>127.0.0.1</HIMCalcServerIP> <HIMCalcServerPort>6001</HIMCalcServerPort> <MulDateLocalCalcServerIP>127.0.0.1</MulDateLocalCalcServerIP> <MulDateLocalCalcServerPort>6002</MulDateLocalCalcServerPort> </Server>解析方式
string GetStingByname(xml_node<>* root, string name, string strRet) {if (!root){return strRet;}xml_node<>* pnode = root->first_node(name.c_str());if (!pnode){return strRet;}return pnode->value(); }int GetIntByname(xml_node<>* root, string name, int nRet) {if (!root){return nRet;}xml_node<>* pnode = root->first_node(name.c_str());if (!pnode){return nRet;}return atoi(pnode->value()); }double GetFloatByname(xml_node<>* root, string name, float fRet) {if (!root){return fRet;}xml_node<>* pnode = root->first_node(name.c_str());if (!pnode){return fRet;}return atof(pnode->value()); } try{string strxmlname = GetExePath() + "\\config\\CommServer.xml";rapidxml::file<> fdoc(strxmlname.c_str());rapidxml::xml_document<> doc;doc.parse<0>(fdoc.data());xml_node<>* root = doc.first_node();m_strCommServerIP = GetStingByname(root, "CommServerIP", "127.0.0.1");m_nCommServerPort = GetIntByname(root, "CommServerPort", 6001);m_strHIMCalcServerIP = GetStingByname(root, "HIMCalcServerIP", "127.0.0.1");m_nHIMCalcServerPort = GetIntByname(root, "HIMCalcServerPort", 6002);m_strMulDateLocalCalcServerIP = GetStingByname(root, "MulDateLocalCalcServerIP", "127.0.0.1");m_nMulDateLocalCalcServerPort = GetIntByname(root, "MulDateLocalCalcServerPort", 6003);doc.clear();}catch (exception &e){CVLog.LogMessage(LOG_LEVEL_ERROR, "load xml error :%s", e.what());}?
總結
以上是生活随笔為你收集整理的rapidxml解析xml文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java升级jdk_JDK版本升级
- 下一篇: SQL SERVER触发器(附有实例)