Tinyxml一一利用tinyxml写入属性及文本信息

有时,我们会遇到复杂的文本修改,这时候该如何操作,如下文本所示:

<FileRoot>
    <ItemGroup id="StringItems">
        <ConfigItem name="MULTIPLEX_MANAGER_CONFIG_FILE" value="C:\Program Files\Qualcomm\QDART\databases\MultiplexManagerConfigFile.xml" />
    </ItemGroup>
    <MULTI_DUT_CONFIG>
        <MULTIPLEXERS>
            <MULTIPLEXER ID="1">
                <!-- Note that CMW100 and CMS can use the same Multiplex Driver -->
                <DRIVER_DLL>C:\Program Files (x86)\Qualcomm\QDART\bin\MCI_QTI_SCPI.dll</DRIVER_DLL>
                <BUS_CONFIG>TCPIP0::192.168.1.6::inst0::INSTR</BUS_CONFIG>
            </MULTIPLEXER>
        </MULTIPLEXERS>
    </MULTI_DUT_CONFIG>
    <DeviceConfigSession>
        <DeviceGroups>
            <DeviceGroup ID="1">
                <Resources>
                    <Resource name="QSEQ_RESOURCE_PATHLOSSMANAGER" config="..\Databases\calDB_NET.xml" />
                </Resources>
                <!--valid tag: %DATE_TIME% %DEVICE_GROUP_ID%-->
                <DeviceLogFileNameTemplate>%SN%_%DATE_TIME%_QSEQ_Log_DeviceGroup_1.txt</DeviceLogFileNameTemplate>
                <Device ID="1" NOTINUSE="false" UUT="True">
                    <Type>QSEQ_DEVICE_QUALCOMM_MOBILE</Type>
                    <ConfigString>CONNECT=QPST:-1:TIMEOUT_SEC:60</ConfigString>
                    <ResponsePayloadConfig>UNCOMPRESSED|ASYNC</ResponsePayloadConfig>
                    <!-- ConfigString Format: "CONNECT=diag_transport_mode:comm_port" -->
                    <!-- where diag_transport_mode = "QPST", "QPHONEMS", or "USER_DEFINED" -->
                    <!-- where comm_port = the COMM port (numeric value) ("-1" for AUTO select or actual COMM port id number -->
                </Device>
                <Device ID="3" NOTINUSE="false" UUT="False">
                    <Type>QSEQ_DEVICE_ROHDESCHWARZ_CMW500</Type>
                    <ConfigString>VISA=TCPIP0::192.168.1.6::inst0::INSTR|RF_PORT_ID=RFAC</ConfigString>
                </Device>
            </DeviceGroup>
        </DeviceGroups>
    </DeviceConfigSession>
</FileRoot>

修改点一:比如我们需要修改第三行ConfigItem对应的value文本:

/*
<FileRoot>
	<ItemGroup id="StringItems">
		<ConfigItem name="MULTIPLEX_MANAGER_CONFIG_FILE" value="C:\Program Files (x86)\Qualcomm\QDART\databases\MultiplexManagerConfigFile.xml" />
	</ItemGroup>
</FileRoot>
*/
bool CTinyxmlEngine::Write_XML_2Floor_Attr(const char*pszPath,const char*pszNode,const char*pszNodeAttrValue,const char*pszNode1,const char*pszNode1AttrName,const char*pszNode1AttrValue)
{
	TiXmlDocument doc(pszPath);
	if (!doc.LoadFile())
	{
		return false;
	}
	const char*pszXmlName;
	TiXmlElement*p_root = doc.RootElement();
	for (TiXmlElement*p_RootElem = p_root->FirstChildElement();p_RootElem;p_RootElem=p_RootElem->NextSiblingElement())
	{
		pszXmlName = p_RootElem->Value();
		if (strcmp(pszXmlName,pszNode)==0)
		{
			for (TiXmlAttribute*p_RootAttr = p_RootElem->FirstAttribute();p_RootAttr;p_RootAttr=p_RootAttr->Next())
			{
				const char *pszName,*pszValue;
				pszName = p_RootAttr->Name();
				pszValue = p_RootAttr->Value();
				if (strcmp(pszValue,pszNodeAttrValue)==0)
				{
					TiXmlElement*p_element = p_RootElem->ToElement();
					for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node=p_node->NextSiblingElement())
					{
						pszXmlName = p_node->Value();
						if (strcmp(pszXmlName,pszNode1)==0)
						{
							TiXmlElement*p_element = p_node->ToElement();
							TiXmlAttribute*p_attribute = p_element->FirstAttribute();
							if (strcmp(p_attribute->Value(),pszNode1AttrName)==0)
							{
								for (TiXmlAttribute*p_attribute = p_element->FirstAttribute();p_attribute;p_attribute=p_attribute->Next())
								{
									pszXmlName = p_attribute->Name();
									if (strcmp(pszXmlName,"value")==0)
									{
										p_attribute->SetValue(pszNode1AttrValue);
										doc.SaveFile();
										return true;
									}
								}		
							}
									
						}
					}
				}
			}
		}
	}
	return false;
}

修改点二:修改MULTI_DUT_CONFIG下的BUS_CONFIG文本:

/*
<FileRoot>
	<MULTI_DUT_CONFIG>
		<MULTIPLEXERS>
			<MULTIPLEXER ID = "1">
				<!-- Note that CMW100 and CMS can use the same Multiplex Driver -->
				<DRIVER_DLL>C:\Program Files (x86)\Qualcomm\QDART\bin\MCI_QTI_SCPI.dll</DRIVER_DLL>	
				<BUS_CONFIG>TCPIP0::192.168.1.3::inst0::INSTR</BUS_CONFIG>
			</MULTIPLEXER>
		</MULTIPLEXERS>
	</MULTI_DUT_CONFIG>
</FileRoot>
*/
bool CTinyxmlEngine::Write_XML_3Floor_Attr(const char*pszPath,const char*pszNode,const char*pszNode1,const char*pszNode2,const char*pszNode2AttrValue,const char*pszName,const char*pszValue)
{
	TiXmlDocument doc(pszPath);
	if (!doc.LoadFile())
	{
		return false;
	}
	const char*pszXmlName;
	TiXmlElement*p_root = doc.RootElement();
	for (TiXmlNode*p_node = p_root->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
	{
		pszXmlName = p_node->Value();
		if (strcmp(pszXmlName,pszNode)==0)
		{
			TiXmlElement*p_element = p_node->ToElement();
			for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node=p_node->NextSiblingElement())
			{
				pszXmlName = p_node->Value();
				if (strcmp(pszXmlName,pszNode1)==0)
				{
					TiXmlElement*p_element = p_node->ToElement();
					for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node=p_node->NextSiblingElement())
					{
						pszXmlName = p_node->Value();
						if (strcmp(pszXmlName,pszNode2)==0)
						{
							TiXmlElement*p_element = p_node->ToElement();
							TiXmlAttribute*p_attribute = p_element->FirstAttribute();
							if (strcmp(p_attribute->Value(),pszNode2AttrValue)==0)
							{
								for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
								{
									pszXmlName=p_node->Value();
									if (strcmp(pszXmlName,pszName)==0)
									{
										TiXmlNode*node =p_node->FirstChild();
										TiXmlText text(pszValue);
										p_node->ReplaceChild(node,text);
										doc.SaveFile();
										return true;
									}
								}
							}
						}	
					}
				}
			}
		}
	}
	return false;
}

修改点三:修改DeviceConfigSession下Device ID="3"属性下的ConfigString文本:

/*
<FileRoot>
	<DeviceConfigSession>
		<DeviceGroups>
		  <DeviceGroup ID="1">
			<Resources>
			  <Resource name ="QSEQ_RESOURCE_PATHLOSSMANAGER" config="..\Databases\calDB_NET.xml"></Resource>
			</Resources>
			<!--valid tag: %DATE_TIME% %DEVICE_GROUP_ID%-->
			<DeviceLogFileNameTemplate>%SN%_%DATE_TIME%_QSEQ_Log_DeviceGroup_1.txt</DeviceLogFileNameTemplate>
			<Device ID="1" NOTINUSE="false" UUT="True">
			  <Type>QSEQ_DEVICE_QUALCOMM_MOBILE</Type>
			  <ConfigString>CONNECT=QPST:-1:TIMEOUT_SEC:60</ConfigString>
			  <ResponsePayloadConfig>UNCOMPRESSED|ASYNC</ResponsePayloadConfig>
			  <!-- ConfigString Format: "CONNECT=diag_transport_mode:comm_port" -->
			  <!-- where diag_transport_mode = "QPST", "QPHONEMS", or "USER_DEFINED" -->
			  <!-- where comm_port = the COMM port (numeric value) ("-1" for AUTO select or actual COMM port id number -->
			</Device>
			<Device ID="3" NOTINUSE="false" UUT="False"> 
			   <Type>QSEQ_DEVICE_ROHDESCHWARZ_CMW500</Type>
			  <ConfigString>VISA=TCPIP0::192.168.1.3::inst0::INSTR|RF_PORT_ID=RFAC</ConfigString>
			</Device>
		  </DeviceGroup>
		</DeviceGroups>
	</DeviceConfigSession>
</FileRoot>
*/
bool CTinyxmlEngine::Write_XML_4Floor_Attr(const char*pszPath,const char*pszNode,const char*pszNode1,const char*pszNode2,const char*pszNode2AttrValue,const char*pszNode3,const char*pszNode3AttrValue,const char*pszName,const char*pszValue)
{
	TiXmlDocument doc(pszPath);
	if (!doc.LoadFile())
	{
		return false;
	}
	const char*pszXmlName;
	TiXmlElement*p_root = doc.RootElement();
	for (TiXmlNode*p_node = p_root->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
	{
		pszXmlName = p_node->Value();
		if (strcmp(pszXmlName,pszNode)==0)
		{
			TiXmlElement*p_element = p_node->ToElement();
			for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node=p_node->NextSiblingElement())
			{
				pszXmlName = p_node->Value();
				if (strcmp(pszXmlName,pszNode1)==0)
				{
					TiXmlElement*p_element = p_node->ToElement();
					for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node=p_node->NextSiblingElement())
					{
						pszXmlName = p_node->Value();
						if (strcmp(pszXmlName,pszNode2)==0)
						{
							TiXmlElement*p_element = p_node->ToElement();
							TiXmlAttribute*p_attribute = p_element->FirstAttribute();
							if (strcmp(p_attribute->Value(),pszNode2AttrValue)==0)
							{
								for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
								{
									pszXmlName=p_node->Value();
									if (strcmp(pszXmlName,pszNode3)==0)
									{
										TiXmlElement*p_element = p_node->ToElement();
										TiXmlAttribute*p_attribute = p_element->FirstAttribute();
										if (strcmp(p_attribute->Value(),pszNode3AttrValue)==0)
										{
											for (TiXmlNode*p_node = p_element->FirstChildElement();p_node;p_node = p_node->NextSiblingElement())
											{
												pszXmlName=p_node->Value();
												if (strcmp(pszXmlName,pszName)==0)
												{
													TiXmlNode*node =p_node->FirstChild();
													TiXmlText text(pszValue);
													p_node->ReplaceChild(node,text);
													doc.SaveFile();
													return true;
												}
											}
										}
									}
								}
							}
						}	
					}
				}
			}
		}
	}
	return false;
}

简单测试程序:

int main()
{
	CTinyxmlEngine m_engine;
	m_engine.Write_XML_2Floor_Attr("Attr_Write.xml","ItemGroup","StringItems","ConfigItem","MULTIPLEX_MANAGER_CONFIG_FILE","C:\\Program Files (x86)\\Qualcomm\\QDART\\databases\\MultiplexManagerConfigFile.xml");
	m_engine.Write_XML_3Floor_Attr("Attr_Write.xml","MULTI_DUT_CONFIG","MULTIPLEXERS","MULTIPLEXER","1","BUS_CONFIG","TCPIP0::192.168.100.1::inst0::INSTR");
	m_engine.Write_XML_4Floor_Attr("Attr_Write.xml","DeviceConfigSession","DeviceGroups","DeviceGroup","1","Device","3","ConfigString","VISA=TCPIP0::192.168.100.1::inst0::INSTR|RF_PORT_ID=RFAC");
	return 0;
}
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<FileRoot>
    <ItemGroup id="StringItems">
        <ConfigItem name="MULTIPLEX_MANAGER_CONFIG_FILE" value="C:\Program Files (x86)\Qualcomm\QDART\databases\MultiplexManagerConfigFile.xml" />
    </ItemGroup>
    <MULTI_DUT_CONFIG>
        <MULTIPLEXERS>
            <MULTIPLEXER ID="1">
                <!-- Note that CMW100 and CMS can use the same Multiplex Driver -->
                <DRIVER_DLL>C:\Program Files (x86)\Qualcomm\QDART\bin\MCI_QTI_SCPI.dll</DRIVER_DLL>
                <BUS_CONFIG>TCPIP0::192.168.100.1::inst0::INSTR</BUS_CONFIG>
            </MULTIPLEXER>
        </MULTIPLEXERS>
    </MULTI_DUT_CONFIG>
    <DeviceConfigSession>
        <DeviceGroups>
            <DeviceGroup ID="1">
                <Resources>
                    <Resource name="QSEQ_RESOURCE_PATHLOSSMANAGER" config="..\Databases\calDB_NET.xml" />
                </Resources>
                <!--valid tag: %DATE_TIME% %DEVICE_GROUP_ID%-->
                <DeviceLogFileNameTemplate>%SN%_%DATE_TIME%_QSEQ_Log_DeviceGroup_1.txt</DeviceLogFileNameTemplate>
                <Device ID="1" NOTINUSE="false" UUT="True">
                    <Type>QSEQ_DEVICE_QUALCOMM_MOBILE</Type>
                    <ConfigString>CONNECT=QPST:-1:TIMEOUT_SEC:60</ConfigString>
                    <ResponsePayloadConfig>UNCOMPRESSED|ASYNC</ResponsePayloadConfig>
                    <!-- ConfigString Format: "CONNECT=diag_transport_mode:comm_port" -->
                    <!-- where diag_transport_mode = "QPST", "QPHONEMS", or "USER_DEFINED" -->
                    <!-- where comm_port = the COMM port (numeric value) ("-1" for AUTO select or actual COMM port id number -->
                </Device>
                <Device ID="3" NOTINUSE="false" UUT="False">
                    <Type>QSEQ_DEVICE_ROHDESCHWARZ_CMW500</Type>
                    <ConfigString>VISA=TCPIP0::192.168.100.1::inst0::INSTR|RF_PORT_ID=RFAC</ConfigString>
                </Device>
            </DeviceGroup>
        </DeviceGroups>
    </DeviceConfigSession>
</FileRoot>

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值