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();
}