TinyXML 简单实用方法附代码实例
2011-11-11 10:41
399 查看
XML文件是以下格式:
这样类似的结构相信与真实使用场景很类似:
阅读XML文件的CPP如下:
<?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; }
相关文章推荐
- 对象,对象集合的简单Xml序列化与反序列化 【实用代码,含泛型方法】
- 使用纯JS代码判断字符串中有多少汉字的实现方法(超简单实用)
- PHP分多步骤填写发布信息的简单方法实例代码
- php生成xml简单实例代码
- Ajax的简单实用实例代码
- C++ tinyxml的编译及简单使用方法
- php生成xml简单实例代码
- php生成xml简单实例代码
- php生成xml简单实例代码
- Web Service 简单实例(java 版本) ,IDE自动生成客户端代码方法
- php生成xml简单实例代码
- PHP分多步骤填写发布信息的简单方法实例代码
- 基于vue2.0版本的手机端mint-ui 的Loadmore上拉刷新下拉加载的方法,对于初学者来说很有帮助,开始写走了很多弯路,网上找到这个方法,简单实用,不多说看代码……
- php生成xml简单实例代码
- php生成xml简单实例代码
- Android 代码写控件代替XML简单实例
- Log4j简单实用例子 Log4j xml配置方法
- 四大常见XML类型的生成和解析方法详解(介绍+优缺点对比+示例+说明)==> 简单实用
- php生成xml简单实例代码
- Log4j简单实用例子 Log4j xml配置方法