<?xml version="1.0" encoding="utf-8"?>
<Organization>
<Devices>
<Device id="1000006" type="201">
<UnitNodes index="0" channelnum="2" >
<Channel id="1000006$2$0$0" name="TV1" />
<Channel id="1000006$2$0$1" name="TV2" />
</UnitNodes>
</Device>
</Devices>
</Organization>
代碼
void IterChannelNode(const TiXmlNode* nodeUnit, std::vector<struChannelInfo> &vecChannelInfo)
{
const TiXmlNode* nodeChannelIter = NULL;
nodeChannelIter = nodeUnit->IterateChildren("Channel", nodeChannelIter);
do
{
if (NULL == nodeChannelIter) break;
std::string strName = nodeChannelIter->ToElement()->Attribute("name");
struChannelInfo channelInfo;
channelInfo.strChannelId = nodeChannelIter->ToElement()->Attribute("id");
channelInfo.strChannelName = nodeChannelIter->ToElement()->Attribute("name");
vecChannelInfo.push_back(channelInfo);
nodeChannelIter = nodeChannelIter->NextSibling();
} while (1);
}
void IterUnitNode(const TiXmlNode* nodeDevice, std::vector<struChannelInfo> &vecChannelInfo)
{
const TiXmlNode* nodeUnitIter = NULL;
nodeUnitIter = nodeDevice->IterateChildren("UnitNodes", nodeUnitIter);
do
{
if (NULL == nodeUnitIter) break;
IterChannelNode(nodeUnitIter, vecChannelInfo);
nodeUnitIter = nodeUnitIter->NextSibling();
} while (1);
}
int IterDeviceInfo(const std::string &strXML, std::vector<struChannelInfo> &vecChannelInfo)
{
if (strXML.empty()) return -1;
TiXmlDocument doc;
doc.LoadFile(strXML.c_str());
TiXmlElement* root = doc.FirstChildElement();
if (NULL == root) return -1;
const TiXmlNode* nodeDevices = NULL;
nodeDevices = root->IterateChildren("Devices", nodeDevices);
if (NULL == nodeDevices) return -1;
const TiXmlNode* nodeDevice = NULL;
nodeDevice = nodeDevices->IterateChildren("Device", nodeDevice);
do
{
if (NULL == nodeDevice) break;
//訪問第一個節點
std::string strTypeId = nodeDevice->ToElement()->Attribute("type");
std::cout << strTypeId << std::endl;
IterUnitNode(nodeDevice, vecChannelInfo);
nodeDevice = nodeDevice->NextSibling();
} while (1);
return 0;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。