您的位置:首页 > 编程语言

TinyXML 简单实用方法附代码实例

2011-11-11 10:41 399 查看
XML文件是以下格式:

<?xml version="1.0" encoding="utf-8"?>
<IPsecConf PolicyName="SecurityPolicy" ActionName="SecurityAction" FilterListName="SecurityList">
<Rule id = "1">
<dirty set = "true"> add </dirty>
<name>RuleModel1</name>
<action>Block</action>
<srcaddr>me</srcaddr>
<dstaddr>any</dstaddr>
<srcport>0</srcport>
<dstport>NULL</dstport>
<description>Block any port</description>
</Rule>
<Rule id = "2" >
<dirty set = "true"> add </dirty>
<name>Rule Model 2</name>
<action>Permit</action>
<srcaddr>me</srcaddr>
<dstaddr>any</dstaddr>
<srcport>3389</srcport>
<dstport>NULL</dstport>
<description>在服务器上开发一个端口允许任何机器访问</description>
</Rule>
<Rule id="3">
<dirty set = "true"> add </dirty>
<name>Rule Model 3</name>
<action>Permit</action>
<srcaddr>me</srcaddr>
<dstaddr>{WebCloudIP}</dstaddr>
<srcport>NULL</srcport>
<dstport>{WebCloudPort}</dstport>
<description>允许服务器自身,访问Web云的IP和端口</description>
</Rule>
</IPsecConf>


这样类似的结构相信与真实使用场景很类似:

阅读XML文件的CPP如下:

#include <string>
#include <iostream>
#include "tinyxml.h"

using namespace std;

TiXmlDocument *pXmlDoc = NULL;
TiXmlElement *pRootEle = NULL;
TiXmlElement *pRule = NULL;
TiXmlElement *pDirty = NULL;
TiXmlElement *pRuleName = NULL;
TiXmlElement *pRuleAction = NULL;
TiXmlElement *pSrcAddr = NULL;
TiXmlElement *pDstAddr = NULL;
TiXmlElement *pSrcPort = NULL;
TiXmlElement *pDstPort = NULL;
TiXmlElement *pDescript = NULL;
TiXmlAttribute *pDirtySet = NULL;
TiXmlAttribute *pRuleidAttr = NULL;
TiXmlAttribute *pPolicyName = NULL;
TiXmlAttribute *pActionName = NULL;
TiXmlAttribute *pFilterList = NULL;

template<class T> bool CheckPointer(T p, string strtitle)
{
if (!p)
{
std::cerr<< "get rule's "
<< strtitle
<< " is failed."
<< endl;

delete pXmlDoc;
pXmlDoc = NULL;

return false;
}

std::cout<< strtitle
<< "'s value Is:"
<< p->Value()
<< "."
<< endl;

return true;
}

bool CheckPointer(TiXmlElement *p, string strtitle)
{
if (!p)
{
std::cerr<< "get rule's "
<< strtitle
<< " is failed."
<< endl;

delete pXmlDoc;
pXmlDoc = NULL;

return false;
}

std::cout<< strtitle
<< "'s value Is:"
<< p->FirstChild()->Value()
<< "."
<< endl;

return true;
}

int wmain(int argc, wchar_t** argv)
{
string strRulesFile("D:\\Rules.xml");

pXmlDoc = new TiXmlDocument(strRulesFile.c_str());
if (!CheckPointer(pXmlDoc, string("pXmlDoc")))
{
return -1;
}

if (!pXmlDoc->LoadFile())
{
std::cerr<< "load file is failed." << endl;
return -1;
}

pRootEle = pXmlDoc->RootElement();
if (!pRootEle)
{
return -1;
}

pPolicyName = pRootEle->FirstAttribute();
if (!CheckPointer(pPolicyName, string("Policy")))
{
return -1;
}

pActionName = pPolicyName->Next();
if (!CheckPointer(pActionName, string("Action")))
{
return -1;
}

pFilterList = pActionName->Next();
if (!CheckPointer(pFilterList, string("Filterlist")))
{
return -1;
}

pRule = pRootEle->FirstChildElement();
if (!pRule)
{
return -1;
}

while (pRule)
{
std::cout<< "......................................." << endl;

pRuleidAttr = pRule->FirstAttribute();
if (!CheckPointer(pRuleidAttr, string("RuleidAttr")))
{
return -1;
}

pDirty = pRule->FirstChildElement();
if (!CheckPointer(pDirty, string("Dirty")))
{
return -1;
}

pDirtySet = pDirty->FirstAttribute();
if (!CheckPointer(pDirtySet, string("DirtyAttr")))
{
return -1;
}

pRuleName = pDirty->NextSiblingElement();
if (!CheckPointer(pRuleName, string("RuleName")))
{
return -1;
}

pRuleAction = pRuleName->NextSiblingElement();
if (!CheckPointer(pRuleAction, string("RuleAction")))
{
return -1;
}

pSrcAddr = pRuleAction->NextSiblingElement();
if (!CheckPointer(pSrcAddr, string("SrcAddr")))
{
return -1;
}

pDstAddr = pSrcAddr->NextSiblingElement();
if (!CheckPointer(pDstAddr, string("DstAddr")))
{
return -1;
}

pSrcPort = pDstAddr->NextSiblingElement();
if (!CheckPointer(pSrcPort, string("SrcPort")))
{
return -1;
}

pDstPort = pSrcPort->NextSiblingElement();
if (!CheckPointer(pDstPort, string("DstPort")))
{
return -1;
}

pDescript = pDstPort->NextSiblingElement();
if (!CheckPointer(pDescript, string("Descript")))
{
return -1;
}

pRule = pRule->NextSiblingElement();
std::cout<< "......................................." << endl;
}

system("pause");

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: