TinyXml2解析xml用法例子

1、认识

在使用之前,先大致了解一下TinyXml2结构,只有清楚地知道这些类都指什么内容,那么在解析任何xml时,都可以很好利用TinyXml2中提供的API。

比如如下的xml:

<?xml version="1.0" encoding="UTF-8"?>
<phonebook>
   <!--one item behalfs one contacted person.-->
   <item>
     <name>sister</name>
     <addr>Shaanxi Xi'an</addr>
     <tel>13759911917</tel>
     <email>miaomiao@home.com</email>
   </item>
   <item>
     <name>xiaoming</name>
     <addr>Liaoning Shenyang</addr>
     <tel>15840330481</tel>
     <email>xiaoming@qq.com</email>
   </item>
   <!--more contacted persons.-->
</phonebook>

结点说明:

TiXmlDeclaration 指的是<?xml version=”1.0″ encoding=”UTF-8″?>,

TiXmlComment 指的是<!–one item behalfs one contacted person.–>、 <!–more contacted persons.–>,

TiXmlDocument 指的是整个xml文档,

TiXmlElement 指的是<phonebook>、<item>、<name>、<addr>等等这些节点,

TiXmlText 指的是‘gougou’、‘15840330481’这些夹在<item>与</item>、<name>与</name>、<addr>与</addr>之间的文本文字,

TiXmlAttribute 指的是<?xml version=”1.0″ encoding=”UTF-8″?>节点中version、encoding,

除此之外就是 TiXmlUnknown。

2、使用

(1)最简单使用

/*
//xml格式:
<ResponseStatus version=“1.0” xmlns=”urn:skylight”>
    <statusCode>XXX</statusCode>
    <statusString>XXX</statusString>
</ResponseStatus>
*/

do
{
  const char * lpXmlData = response_data;    //需要解析的数据buffer
  int32_t      llXmlSize = response_data_size;  //需要解析的size

   XMLProtocal       xml;
   int avx_success = xml.Open(lpXmlData, llXmlSize);
   if (avx_success == 0)
   {
       printf("open fail!\n");
       break;
    }
    XMLElement * ResponseStatus = xml.RootElement();
    if (ResponseStatus == NULL)
    {
	break;
    }
    int stateCode = -1;

   XMLElement* statusCode = ResponseStatus->FirstChildElement("statusCode");
    if (statusCode == NULL ||  == NULL)
    {
	//...;
    }
    else
   {
	stateCode = atoi(statusCode->GetText());
	
   }
   XMLElement* statusString = ResponseStatus->FirstChildElement("statusString");
   if (statusString == NULL || statusString->GetText() == NULL)
   {
	//...;
   }
   else
   {
       //使用 statusString->GetText()	
   }

} while (0);

(2)使用XmlDocument ,纯xml文件

/*
xml格式:
*/
/*
 <tcphost>xxx</tcphost>
 <tcpport>xxx</tcpport>
 <udphost>xxx</udphost>
 <udppost>xxx</udpport>
*/
do
{
	const char * lpXmlData = data;
	int32_t      llXmlSize = data_size;

	XMLProtocal       xmlen;
	int avx_success = xmlen.Open(lpXmlData, llXmlSize);
	if (avx_success == 0){
		printf("open fail!\n");
		break;
	}
	XMLDocument * doc;
	doc = xmlen.GetDocument();
	const char* TcpHost = doc->FirstChildElement("tcphost")->GetText();
	const char* TcpPort = doc->FirstChildElement("tcpport")->GetText();
	const char* UdpHost = doc->FirstChildElement("udphost")->GetText();
	const char* UdpPort = doc->FirstChildElement("udpport")->GetText();

	if (TcpHost)
	{
	}
	if (TcpPort)
	{
	}
	if (UdpHost)
	{
	}
	if (UdpPort)
	{
	}
} while (0);

(3)多级多层解析

/*
xml格式:
<homeInfo version = "1.0" xmlns = "urn:skylight">
<deviceList>
<device>
<deviceId>XXX<deviceId>
<deviceType>XXX</Type>
<deviceModel>XXX</deviceModel>
<deviceName>XXX</deviceName>
<deviceStatus>XXX</deviceStatus>
<button>
<buttonName> XXX </buttonName>
<buttonStatus> XXX </buttonStatus>
<leftButton> XXX </leftButton>
<leftStatus> XXX </leftStatus>
<rightButton> XXX </rightButton>
<rightStatus> XXX </rightStatus>
</button>
</device>
... ...
</deviceList>
<ResponseStatus>
<statusCode>XXX</statusCode>
<statusString>XXX</statusString>
</ResponseStatus>
</homeInfo>

*/
//解析xml
do
{
	const char * lpXmlData = data;
	int32_t      llXmlSize = data_size;

	XMLProtocal       xml;
	int avx_success = xml.Open(lpXmlData, llXmlSize);
	if (avx_success == 0)
	{
		printf("open fail!\n");
		break;
	}
	XMLElement * homeInfo = xml.RootElement();
	if (homeInfo == NULL)
	{
		break;
	}
	int state = -1;
	XMLElement* ResponseStatus = homeInfo->FirstChildElement("ResponseStatus");
	if (ResponseStatus)
	{
	XMLElement* statusCode = ResponseStatus->FirstChildElement("statusCode");
	if (statusCode == NULL || statusCode->GetText() == NULL)
	{
	}
	else
	{
	     state = atoi(statusCode->GetText());
	}
	XMLElement* statusString = ResponseStatus->FirstChildElement("statusString");
	if (statusString == NULL || statusString->GetText() == NULL)
	{
	}
	else
	{
	}
	}
	if (state == 0)  //正确响应
	{
	     XMLElement* deviceList = homeInfo->FirstChildElement("deviceList");
	    if (deviceList == NULL) break;

	    XMLNode* device = deviceList->FirstChild();
	    while (device)
	    {

		XMLElement* deviceId = device->FirstChildElement("deviceId");
		if (deviceId && deviceId->GetText())
		{
		}
		XMLElement* deviceType = device->FirstChildElement("deviceType");
		if (deviceType && deviceType->GetText())
		{
		}
		XMLElement* deviceModel = device->FirstChildElement("deviceModel");
		if (deviceModel && deviceModel->GetText())
		{
		}
		XMLElement* deviceName = device->FirstChildElement("deviceName");
		if (deviceName && deviceName->GetText())
		{
		}
			
		XMLElement* deviceStatus = device->FirstChildElement("deviceStatus");
		if (deviceStatus && deviceStatus->GetText())
		{
			
		}
		/*
		button
		*/
		XMLElement* button = device->FirstChildElement("button");
		if (button)
		{
		XMLElement* buttonName = button->FirstChildElement("buttonName");
		if (buttonName && buttonName->GetText())
		{
		}
		XMLElement* buttonStatus = button->FirstChildElement("buttonStatus");
		if (buttonStatus && buttonStatus->GetText())
		{
		}
		XMLElement* leftButton = button->FirstChildElement("leftButton");
		if (leftButton && leftButton->GetText())
		{
		}
		XMLElement* leftStatus = button->FirstChildElement("leftStatus");
		if (leftStatus && leftStatus->GetText())
		{
		}
		XMLElement* rightButton = button->FirstChildElement("rightButton");
		if (rightButton && rightButton->GetText())
		{
		}
                XMLElement* rightStatus = button->FirstChildElement("rightStatus");
		if (rightStatus && rightStatus->GetText())
		{
		}
		}
	 device = device->NextSibling();
		}
	}
} while (0);

/*
<shareList>
<shareId>12345y</shareId>
<shareId>12345u</shareId>
<shareId>12345v</shareId>
</shareList>
*/

XMLElement * shareList = queryShareRemain->FirstChildElement("shareList");
if (shareList == NULL) return false;

XMLNode * shareId = shareList->FirstChildElement();
int i = 0;
while (shareId)
{
	XMLElement * shareIdElement = shareId->ToElement();
	if (shareIdElement && shareIdElement->GetText())
	{
		lpShareRemain->kSharedFileList.push_back(shareIdElement->GetText());
	}		
	shareId = shareId->NextSiblingElement();
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值