rapidxml解析xml文档

本文介绍了如何在C++项目中使用 Rapidxml 库解析XML文档,包括 roadlist 和 rfidlist 节点的数据读取,展示了如何处理不同节点属性并创建 RoadInfo 和 RFIDInfo 结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将三个.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());
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值