加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

tinyxml2解析XML文件

发布时间:2020-12-16 05:56:29 所属栏目:百科 来源:网络整理
导读: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*
	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");
	}

	//解析文档
	// HOST
	tinyxml2::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 );

	// DB
	root = 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;

	// SFTP
	root = 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;

	// Path
	root = xmlDoc.FirstChildElement( "config" )->FirstChildElement( "path" );
	resultPath = root->FirstChildElement( "result" )->GetText();
	reportPath = root->FirstChildElement( "report" )->GetText();

	// Resend Pack
	root = 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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读