微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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");
	}

	//解析文档
	// 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>

原文地址:https://www.jb51.cc/xml/295532.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。