tinyxml2解析XML文件
生活随笔
收集整理的這篇文章主要介紹了
tinyxml2解析XML文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
tinyxml2::XMLDocument xmlDoc;tinyxml2::XMLError error = xmlDoc.LoadFile(cfgFile.c_str());if (error != tinyxml2::XML_NO_ERROR) {throw ConfigurationFileNotFound("Configuration File is failed to open");}//解析文檔// HOSTtinyxml2::XMLElement* root = xmlDoc.FirstChildElement("config")->FirstChildElement( "web");const char * contextStr = root->FirstChildElement("host")->GetText();hostConfig.host = contextStr;contextStr = root->FirstChildElement( "port" )->GetText();hostConfig.port = atoi( contextStr );// DBroot = xmlDoc.FirstChildElement("config")->FirstChildElement("db");contextStr = root->FirstChildElement("host")->GetText();dbConfig.host = contextStr;contextStr = root->FirstChildElement("port")->GetText();dbConfig.port = atoi( contextStr );contextStr = root->FirstChildElement("username")->GetText();dbConfig.username = contextStr;contextStr = root->FirstChildElement("password")->GetText();dbConfig.password = contextStr;// SFTProot = xmlDoc.FirstChildElement( "config" )->FirstChildElement( "sftp" );contextStr = root->FirstChildElement( "host" )->GetText();sftpConfig.host = contextStr;contextStr = root->FirstChildElement("username")->GetText();sftpConfig.username = contextStr;contextStr = root->FirstChildElement("password")->GetText();sftpConfig.password = contextStr;contextStr = root->FirstChildElement("remote")->GetText();sftpConfig.remote= contextStr;contextStr = root->FirstChildElement("local")->GetText();sftpConfig.local = contextStr;// Pathroot = xmlDoc.FirstChildElement( "config" )->FirstChildElement( "path" );resultPath = root->FirstChildElement( "result" )->GetText();reportPath = root->FirstChildElement( "report" )->GetText();// Resend Packroot = xmlDoc.FirstChildElement( "config" )->FirstChildElement( "multipack" );multiPackResendConfig.timeout = atoi(root->FirstChildElement("timeout")->GetText());multiPackResendConfig.maxcount = atoi(root->FirstChildElement("maxcount")->GetText());root = xmlDoc.FirstChildElement( "config" )->FirstChildElement( "singlepack" );singlePackResendConfig.timeout = atoi(root->FirstChildElement("timeout")->GetText());singlePackResendConfig.maxcount = atoi(root->FirstChildElement("maxcount")->GetText());
下面是xml文件
<config><web><host>172.16.6.70</host><port>5055</port></web><db><host>172.16.6.78</host><port>5432</port><username>postgres</username><password>postgres</password><dbname>RemoteAssert</dbname></db><sftp><host>172.16.6.78</host><username>root</username><password>*********</password><remote>/opt</remote><local>/home/liujiayu/diagnosefile/cmdanddatafile</local></sftp><path><result>/home/liujiayu/diagnosefile/result</result><report>/home/liujiayu/diagnosefile/report</report></path><multipack><timeout>10</timeout><maxcount>3</maxcount></multipack><singlepack><timeout>3</timeout><maxcount>3</maxcount></singlepack> </config>
總結
以上是生活随笔為你收集整理的tinyxml2解析XML文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 棋牌游戏服务器架构: 详细设计(三) 数
- 下一篇: DOM解析XML